objective-c - NSNumber 与 NSInteger 与 NSObject 属性的 int

标签 objective-c ios

我们的 iOS 中有一个带有 id 属性的模型。这是我们当前使用的(顺便说一句,这是 iOS 5)。

 @property (nonatomic, assign) int userID;

到目前为止似乎工作正常。我想知道这是否会导致任何问题。

示例:我理解这意味着 ID 属性本身无法存储到 plist 中。但是,这是 NSObject 的属性。如果我们将任何内容存储到文件/核心数据/nsuserdefaults/无论什么,它可能是整个对象而不仅仅是这个属性。

我想我的问题是......我们将其存储为 int 而不是 NSNumber 是否会给自己带来任何问题?

其次,将其存储为 NSInteger 有什么区别。我知道这只是 long 或 int 的类型定义,具体取决于体系结构。由于我们只针对 iPhone,因此将它设置为 int 是否重要?在那种情况下似乎没有任何区别。

最佳答案

I guess my question is ... are we going to cause ourselves any problems by storing this as an int as opposed to an NSNumber?

这实际上取决于您要使用该值做什么。如果你想把它当作一个对象(例如,这样你就可以将它存储在 NSArray 或 NSDictionary 中)NSNumber 可能会很方便。如果您只想跟踪值,并且 int 适合您,那么使用 int 就可以了。

Secondly, what would be the difference in storing this as an NSInteger instead. I understand that it's just a type def to either long or int depending in the architecture. Since we're only targeting iPhone does it matter that it's just set to int?

我会选择 NSInteger(和 NSUInteger)。使用这些类型,您的代码将自动为您正在编译的体系结构使用适当的大小。你可能只针对 iOS,但你可能在 iOS 模拟器上运行你的代码,而 iOS 模拟器在 MacOS X 上运行。所以这是两种架构——你不知道 iOS 将来会发生什么。

关于objective-c - NSNumber 与 NSInteger 与 NSObject 属性的 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9527648/

相关文章:

ios - UIViewController 中的 UITableView 不显示内容

objective-c - UIView图层圆角和-drawRect :

iphone - 如何为 UISlider 轨道使用不可拉伸(stretch)但重复的背景图像?

iOS 可能在 setter 方法中传递 NSArray

objective-c - 关闭“使用自动版式”会导致应用仅以纵向模式运行

ios - 横向模式下的 UIImagePicker - 如何旋转 UIAlertViews?

ios - didRangeBeacons 一次只返回一个 iBeacons

ios - iOS:当我从iMazing打开我的应用程序时,如何隐藏和防止读取,以及如何防止在Mac上复制特定文件夹?

objective-c - Xcode 警告 "Property access results unused - getters should not be used for side effects"

ios - xCode上的“复制期间的 strip 调试符号”和 “Strip Linked Product”的区别