I am writing an application using Core Data which heavily depends on setting attributes from string values. However, I am running into a problem because Core Data, when it creates wrapper methods, uses NSNumber to represent all of the numeric fields. Therefore, if I pass in a String using key/value coding setValue:forKey: it gives me a type error:
For instance, I have an object type "Building". In the datamodel, I have an attribute called "fbFloors" set to integer 32.
Then, in my code, I do the following:
Building * b = [NSEntityDescription insertNewObjectForEntityForName:@"Building" inManagedObjectContext:ctx]; [b setValue:@"2" forKey:@"fbFloors"];
This raises the following exception:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for attribute: property = "fbFloors"; desired type = NSNumber; given type = __NSCFConstantString; value = 2.'
I would like it to coerce the string value to a number, but it doesn't want to. I tried implementing coerceValueForFbFloors: but it looks like that is only available in Mac OS X, not iOS.
Any ideas?