xcode 在核心数据中存储 NSInteger

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7044816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:43:45  来源:igfitidea点击:

Store NSInteger in Core Data

xcodecore-datansnumbernsinteger

提问by Andoriyu

Is there any way I can skip dealing with NSNumber and work directly with NSInteger?

有什么办法可以跳过处理 NSNumber 并直接使用 NSInteger 吗?

回答by ndfred

Core Data will only allow NSNumbers. However, you can write custom getters and setters to use NSInteger properties. mogeneratoris a wonderful tool that does that automatically for you: it generates classes with native properties for all your entities.

核心数据将只允许 NSNumbers。但是,您可以编写自定义 getter 和 setter 以使用 NSInteger 属性。mogenerator是一个很棒的工具,它会自动为您执行此操作:它为您的所有实体生成具有本机属性的类。

回答by Evan Mulawski

No. NSIntegeris just a typedef for a long integer, not an object.

NSInteger,只是一个长整数的 typedef,而不是一个对象。

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 NSNumberclass allows the encapsulation of primitive types (int, float, etc.) into an object, which can then be stored into Property Lists and Core Data.

NSNumber类允许原始类型(的封装intfloat等)到一个对象,然后可以被存储到属性列表和核心数据。

Example:

例子:

float pi = 3.1415;
NSNumber *piNumber = [NSNumber numberWithFloat:pi];

You can then easily access and/or transform the value stored into the NSNumberobject:

然后,您可以轻松访问和/或转换存储到NSNumber对象中的值:

int piAsInteger = [piNumber intValue];