开发者

How can I read Float from Core Data / iPhone?

开发者 https://www.devze.com 2023-02-11 06:09 出处:网络
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.

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

精彩评论

暂无评论...
验证码 换一张
取 消