Problems with Float on core-data

依然范特西╮ 提交于 2020-01-06 05:28:46

问题


The following code largely inspired by some example I found on the net seems to work fine, with the core data entity called "Contact" and the property called "address" having an attribute String, in the xcdatamodel. It saves my data with no problem. Now my question is : how do I need to modify this code ? In order to make it work after I change the attribute of the property "address" from String to Float in the xcdatamodel.

CoreDataTestOneAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *newContact;
newContact = [NSEntityDescription insertNewObjectForEntityForName:@"Contacts" inManagedObjectContext:context];
[newContact setValue:address_InputField.text forKey:@"address"];
NSError *error;
[context save:&error];

回答1:


To store a float in a Core Data float attribute, wrap it in a NSNumber object like this:

[newContact setValue:[NSNumber numberWithFloat:floatValue] forKey:@"address"];



回答2:


This is a guess, but I think you will need to wrap that float in a NSNumber. numberWithFloat:

Creates and returns an NSNumber object containing a given value, treating it as a float.

+ (NSNumber *)numberWithFloat:(float)value


来源:https://stackoverflow.com/questions/5038565/problems-with-float-on-core-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!