I’m iterating through an array of numbers that come through an API call:
[6000, 6001, 2000] for example
Here’s my code:
for(NSNumber* arrayID in array){
NSManagedObject *ent = [NSEntityDescription insertNewObjectForEntityForName:@"Genre" inManagedObjectContext:self.managedObjectContext];
[ent setValue:arrayID forKey:@"genreID"];
[ent setValue:name forKey:@"genreName"];
[mySet addObject:ent];
}
When setting the arrayID (which is NSNumber in my core data), I get a crash:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for attribute: property = "genreID"; desired type = NSNumber; given type = __NSCFString; value = 6002.'
It’s as if arrayID has not been cast as an NSNumber? Xcode says its an NSNumber when I type it in, but if I put [arrayID doubleValue] for example, Xcode tells me that ‘doubleValue cannot be sent for type ‘id’
Any ideas?
Just because you declared
arrayIDas anNSNumberpointer, it doesn’t guarantee that thearrayIDobject is actually anNSNumber. In this case, the array appears to consist ofNSStrings.In Objective-C, the object types are only really hints to the compiler, you can cast anything to anything, and call any method on an object, these things only actually fail at runtime. You’ll have to look at where
arraywas created to see what the type actually is.In this case, if they are all in fact strings, you can do