Michel
Michel

Reputation: 11749

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 in an NSstring ?

Upvotes: 1

Views: 1344

Answers (2)

davidstites
davidstites

Reputation: 687

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]];

Upvotes: 0

Matthias Bauch
Matthias Bauch

Reputation: 90117

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.

Upvotes: 5

Related Questions