问题
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