NSNumber versus NSInteger

If you’ve ever found yourself scratching your head thinking “now which one should I be using, NSNumber or NSInteger?” the short summary below should help.

NSInteger is nothing more than a synonym for a long integer. What follows is how NSInteger is defined:

NSNumber is an Objective-C class, a subclass of NSValue to be specific. You can create an NSNumber object from a signed or unsigned char, short int, int, long int, long long int, float, double or BOOL.

One of the primary distinctions is that you can use NSNumber in collections, such as NSArray, where an object is required. For example, if you need to add a float into an NSArray, you would first need to create an NSNumber object from the float:

NSInteger is a simply an integer, NSNumber is an object.

12 Comments

  1. Hi there. I found this article useful, but have a further question. I converted an int into an NSNumber and was able to successfully save it to my preferences.plist for my application. I am a bit befuddled when it comes to reading the NSNumber back out and converting it into an int again. Yes, I am fairly new to objective C, and am not sure how to cast the NSNumber back into an int.

  2. Chris,

    It depends on how you stored the NSNumber to the plist. For example, if you stored the NSNumber object into an NSArray, you might have something like this:

    NSInteger someInt = [[tmpArray objectAtIndex:0] integerValue];

    In this case I am assuming the NSNumber object was stored in a plist which was an array of objects. I then convert the value at index 0 to an integer (using the method integerValue).

    John

  3. That worked like a charm. Thanks for the speedy answer!

  4. Thanks for this post, cleared a (noob) problem I’ve been having with my NSDictionary.

  5. How do I check on receipt of a variable if it is NSInteger or NSNumber?

    -(void)reportDataType:number{

    if(…) NSLog(@”received NSInteger”);
    if(…) NSLog(@”received NSNumber”);

    }

    • Justin,

      I don’t think it’s possible to write a method as you have shown passing in a “number” as NSNumber is an object and NSInteger is standard C data type (not an object). You could have a method where you pass in an object of type “id” the generic object type and then determine if the object is an NSNumber similar to this:

      I wrote about determining class types here: Determining Class Types

      Hope that helps.

  6. My current solution for such math is :
    value = [NSNumber numberWithInt:[value intValue] + 10];

    but this seems to be slow, memory intensive and ugly…
    how to calc something more complex with that ?

    like :

    value = value * t + (1-t) * ( sin( value * M_PI) )

    value = [NSNumber numberWithFloat:[value floatValue] * t + (1-t) * (sin ( [value floatValue] * M_PI) )];

    *phooo* that stinks!

  7. Hi,
    I am confused of using int or NSInteger. But, thanks for suggesting a better one NSNumber.
    Can we access the NSNumber objects in other classes like NSString objects ?
    ActuallyI need to access the values declared in classB from classA. By using int, float types it is not working. Which is better for this case NSInteger or NSNumber.
    Thank you,
    Please post me answer to mail id if not a problem to you.
    srikanth_rongali@yahoo.co.in

  8. One thing I didn’t see anyone ask here is ‘Why? Why is it necessary to use objects in NSArrays?’ In C++ we can make an array of any type, primitive, class etc without ‘wrapping’ it in another layer of code. Why is it different in Obj-C?

    Anyone know why?

    Thanks

  9. Author: a-ha! Thanks!

    hhamm: yes, it’s slow. If speed is what you need, NSNumber is out of the question.

    Ted: because NSArray auto-retains and auto-releases and does a lot of things which require that whatever you put in there responds to certain protocols (namely those dictated in NSObject).

    -Kalle.

  10. @Ted,

    You can use C-type arrays for primitives and (id).

    id bob[10];

    compiles just fine.

    NSArray provides all the *memory management* associated with handling objects in Obj-C so that

    NSArray *joe = [NSArray arrayWithContentsOfURL: myURL];

    creates all the objects and

    [joe release];

    removes them all from memory. Otherwise, you’d have to:

    for (int i = 0; i < 10; i++) {
    [bob[i] release];
    }

    Hope that helps!

    -S!

Comments are closed.