NSNumber vs. NSInteger vs. int for the NSObject property

We have a model in iOS with the id property. Here is what we are using now (by the way, this is iOS 5).

@property (nonatomic, assign) int userID; 

Everything seems to be working fine. I am wondering if this will lead to further problems.

Example. I understand that this means that the ID property itself cannot be stored in plist. However, this is a property of NSObject. If we store anything in the file / core data / nsuserdefaults / whatever it is, this is the whole object, not just this property.

I guess my question is ... are we going to cause us any problems storing this as an int, not an NSNumber?

Secondly, what's the difference in keeping this as an NSInteger. I understand that it is just a def type for long or int, depending on the architecture. Since we focus only on the iPhone, does it matter that it is just set to int? It seems that in this case it would not have made any difference.

+4
source share
3 answers

I think my question is ... are we going to cause problems by storing this as an int, not an NSNumber?

It really depends on what you are going to do with this value. If you want to treat it as an object (for example, to store it in an NSArray or NSDictionary), NSNumber can be convenient. If you just want to keep track of the value and int works for you, then it's ok to use int .

Secondly, what's the difference in keeping this as an NSInteger instead. I understand that this is just a def type for long or int depending on the architecture. Since we focus only on the iPhone, does it matter that it is just set to int?

I would go with NSInteger (and NSUInteger). Using these types, your code will automatically use the appropriate size for the architecture for which you are compiling. You can focus only on iOS, but you probably use your code on an iOS simulator that runs on MacOS X. So the two architectures are right there - and you don’t know what will happen to iOS in the future.

+6
source

The only limitation I can think of is that int = 0 is a valid value or int that is not null is an important use case.

Since ints are always initialized to 0, you will not have a situation where you can check for the absence of this property.

In your case, say you want to check if user_id is present or not, then this is not possible with a primitive data type such as int, since it will always matter.

In other scenarios, 0 might be a valid value (or even in your scenario - Steve Jobs joked to be Apple employee number 0 in many pop culture references). In this case, an int initialized to 0 each time may be an undesirable side effect that you will have to deal with.

0
source

It is perfectly normal to use int as a property in a subclass of NSObject.

Depending on your platform, NSInteger can be int or 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 does not exceed the int limit.

-one
source

Source: https://habr.com/ru/post/1399305/


All Articles