Is there any way I can skip dealing with NSNumber and work directly with NSInteger?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Core Data will only allow NSNumbers. However, you can write custom getters and setters to use NSInteger properties. mogenerator is a wonderful tool that does that automatically for you: it generates classes with native properties for all your entities.
回答2:
No. NSInteger
is just a typedef for a long integer, not an object.
Actual implementation:
#if __LP64__ || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif
The NSNumber
class allows the encapsulation of primitive types (int
, float
, etc.) into an object, which can then be stored into Property Lists and Core Data.
Example:
float pi = 3.1415;
NSNumber *piNumber = [NSNumber numberWithFloat:pi];
You can then easily access and/or transform the value stored into the NSNumber
object:
int piAsInteger = [piNumber intValue];