Problems with Float by Master Data

The following code, heavily inspired by some example that I found on the network, seems to work fine, with a main data object called "Contact" and a property called "address" with a String attribute in xcdatamodel. This saves my data without any problems. Now my question is: how do I change this code? To make it work after I changed the property attribute "address" from String to Float to 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]; 
0
source share
2 answers

To save a float in the Coreat float attribute, wrap it in an NSNumber object as follows:

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

This is an assumption, but I think you will need to wrap this float in NSNumber. numberWithFloat:

 Creates and returns an NSNumber object containing a given value, treating it as a float. + (NSNumber *)numberWithFloat:(float)value 
0
source

Source: https://habr.com/ru/post/1403616/


All Articles