开发者

Problems with Float on core-data

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 xcdata开发者_开发技巧model. 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];


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


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
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜