Search code examples
iosintegernsnumber

Should I use NSNumber or an int in an iPhone/iPad class?


I'm creating an object that will have two integers (or NSNumbers) and an NSDate as ivars. This object will be in an NSMutableArray. To my knowledge, we cannot put primative integers in an NSMutableArray, but will my object work with ints? The reason I don't want to use NSNumbers is because these will have to be mutable, and I don't really want to create a new NSNumber everytime.

Also, will using ints create problems with iphone architecture differences?


Solution

  • 1) You can safely use ints as ivars because they are wrapped in another object. NSNumber is just a Cocoa class wrapper for the different numeric types.

    2)To be safe, you could use NSUInteger, which is typedefed like this:

    #if __LP64__ || NS_BUILD_32_LIKE_64
    typedef long NSInteger;
    typedef unsigned long NSUInteger;
    #else
    typedef int NSInteger;
    typedef unsigned int NSUInteger;
    #endif
    

    So you will get the correct type for the current architecture.