开发者

Trouble setting number attribute in Core Data

开发者 https://www.devze.com 2023-03-05 13:55 出处:网络
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 model

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 %@)

0

精彩评论

暂无评论...
验证码 换一张
取 消