How can I read Float from Core Data / iPhone?
I have Float values stored in Core-Data. What is the code to use to read these values 开发者_JAVA百科in an NSstring ?
Core-Data uses NSNumber objects to store the float value.
To get the 'raw' float value and put it into a string you would use something like this.
NSNumber *floatNumber = [managedObject valueForKey:@"myFloatValueKey"];
float myFloat = [floatNumber floatValue];
NSString *floatString = [NSString stringWithFormat:@"%f", myFloat];
Maybe a NSNumberFormatter would be useful.
Well, assuming that you mean your Core Data entity has an attribute of type float, you could simply access that field after you perform a fetch of that object.
[[self managedObjectContext] fetchObjectsForEntityName:@"EntityName" withPredicate:
@"(attribute LIKE[c] 'value') AND (attribute2 > %@)", someValue];
You could then put this in string format with this:
NSString* myNewString = [NSString stringWithFormat:@"%f", [[managedObject floatAttribute] floatVal]];
精彩评论