简体   繁体   English

对于NSObject属性,NSNumber与NSInteger对比int

[英]NSNumber vs. NSInteger vs. int for NSObject property

We've got a model in our iOS with an id property. 我们的iOS中有一个带有id属性的模型。 Here's what we're currently using (this is iOS 5 by the way). 这是我们目前使用的(顺便说一下,这是iOS 5)。

 @property (nonatomic, assign) int userID;

Seems to be working fine so far. 到目前为止似乎工作正常。 I'm wondering if this will cause any problems going forward. 我想知道这是否会导致任何问题。

Example: I understand that this means that the ID property itself could not be stored into a plist. 示例:我理解这意味着ID属性本身无法存储到plist中。 However, this is a property of an NSObject. 但是,这是NSObject的属性。 If we were storing anything into a file/core data/nsuserdefaults/whatever it would likely be the entire object and not just this property. 如果我们将任何东西存储到文件/核心数据/ nsuserdefaults /中它可能是整个对象而不仅仅是这个属性。

I guess my question is ... are we going to cause ourselves any problems by storing this as an int as opposed to an NSNumber? 我想我的问题是......我们将这个存储为int而不是NSNumber会导致我们自己出现任何问题吗?

Secondly, what would be the difference in storing this as an NSInteger instead. 其次,将此存储为NSInteger会有什么不同。 I understand that it's just a type def to either long or int depending in the architecture. 据我所知,它只是一个类型def,可以是long或int,具体取决于体系结构。 Since we're only targeting iPhone does it matter that it's just set to int? 因为我们只针对iPhone,所以它只是设置为int? Doesn't seem like it would make any difference in that case. 在这种情况下似乎没有任何区别。

I guess my question is ... are we going to cause ourselves any problems by storing this as an int as opposed to an NSNumber? 我想我的问题是......我们将这个存储为int而不是NSNumber会导致我们自己出现任何问题吗?

That really depends on what you're going to do with this value. 这实际上取决于你将如何处理这个价值。 If you'll want to treat it as an object (for example, so that you can store it in an NSArray or NSDictionary) NSNumber may be convenient. 如果你想把它当作一个对象(例如,你可以将它存储在NSArray或NSDictionary中),NSNumber可能很方便。 If you just want to keep track of the value, and int works for you, then it's fine to use int . 如果你只想跟踪值,并且int适合你,那么使用int

Secondly, what would be the difference in storing this as an NSInteger instead. 其次,将此存储为NSInteger会有什么不同。 I understand that it's just a type def to either long or int depending in the architecture. 据我所知,它只是一个类型def,可以是long或int,具体取决于体系结构。 Since we're only targeting iPhone does it matter that it's just set to int? 因为我们只针对iPhone,所以它只是设置为int?

I'd go with NSInteger (and NSUInteger). 我会选择NSInteger(和NSUInteger)。 Using those types, your code will automatically use the appropriate size for the architecture you're compiling for. 使用这些类型,您的代码将自动使用适合您正在编译的体系结构的大小。 You may only be targeting iOS, but you're probably running your code on the iOS simulator, which runs on MacOS X. So that's two architectures right there -- and you don't know what may happen with iOS in the future. 你可能只针对iOS,但你可能在iOS模拟器上运行你的代码,它运行在MacOS X上。所以这就是两个架构 - 你不知道将来iOS会发生什么。

The only limitation I can think of is if int=0 is a valid value or int not having a value (null) is an important use case. 我能想到的唯一限制是,如果int = 0是有效值,或int没有值(null)是一个重要的用例。

Since ints are always initialised with 0, you wont have a situation where you can check for non-existence of that property. 由于整数始终用0初始化,因此您不会检查是否存在该属性。

In your case say you wanna test if user_id is present or not then its not possible with primitive data type like int since it will always have a value. 在您的情况下,如果您想测试user_id是否存在,则无法使用原始数据类型(如int),因为它始终具有值。

In another scenarios 0 could be a valid value (or even in your scenario - Steve Jobs was joked to be Employee Number 0 in Apple in many pop culture references). 在另一种情况下,0可能是一个有效的值(或者甚至在您的场景中 - 在许多流行文化参考中,史蒂夫·乔布斯被戏称为Apple的员工编号0)。 In that case int getting initialized with 0 every time might be an unwanted side-effect you will have to deal with. 在这种情况下,int每次初始化为0可能是一个不必要的副作用,你将不得不处理。

It is quite normal to use an int as a property in a subclass of NSObject. 在NSObject的子类中使用int作为属性是很正常的。

Depending on your platform, NSInteger could be an int or a long but other than that, it doesn't matter if you use int or NSInteger and can be used interchangeably as long as the value doesn't exceed the limit of an int . 根据您的平台, NSInteger可以是intlongNSInteger ,使用intNSInteger并不重要,只要该值不超过int的限制,就可以互换使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM