Trouble setting number attribute in Core Data
I have an Entity "Item" in Core Data. Item has two attributes: name and number. name is an NSString* and number is an NSNumber*. I specified "Integer 32" as the type for number in the Core Data modelling tool. The following code sets the name correctly but not the number:
Item *item = (Item *) [NSEntityDescription insertNewObjectForEntityForName:@"Item"
inManagedObjectContext:managedObjectContext];
[item setName:@"test name"];
[item setNumber:[NSNumber numberWithInteger:123]];
Lo开发者_如何转开发gging card.name shows "test name" as expected. Logging card.number shows a large random number. I was expecting 123 since that's what I passed to numberWithInteger.
Why isn't the number attribute being set correctly?
A few notes:
- I created the Item class by selecting the "Item" Entity in the modeling tool and adding a new "Managed Object Class". This created the properties etc for me.
It was a logging issue:
NSLog([NSString stringWithFormat:@"%d", item.number]);
needed to be
NSLog([NSString stringWithFormat:@"%@", item.number]);
(Had to change %d to %@)
精彩评论