Subscribe ( )

iPhone Programming Tutorials

 

Forum

You must be logged in to post Login Register

Search 

NSInteger vs NSNumber vs int

User Post

2:53 pm
October 6, 2008


Noob

London

posts 2

1

It may seem trivial but please let me ask what is the real difference between using either on the iPhone.

I have read about NSInteger to be the next step for 64 bit architecture which probably has nothing to do with the iPhone and my app but of course I wonder why this difference. Shoudn't int be a native objective C element without any extra need?

And what about NSNumber, i understand I may need it as some Cocoa methods ask for object but what is the performance implications on the iPhone?

Should I try to use int in such a memory scarce system?

Thanks for the time.

12:11 pm
October 10, 2008


Neil

iCoder

North Wales, UK

posts 6

2

As far as the iPhone is concerned, NSInteger and int are interchangable. Take a look at the definition in NSObjCRuntime.h:

#typedef int NSInteger;
#typedef unsigned int NSUInteger;

Obviously from this, NSInteger and int are one and the same.

NSNumber is very different as it provides a way for holding a value as any numeric type, not just ints, but char, short int, long int, long long int, float, double and BOOL. It's your quintessential Swiss Army knife of numeric types. 

12:55 pm
October 17, 2008


Noob

London

posts 2

3

Thanks so much for the clarification.

12:42 pm
November 11, 2008


chrizbo

Noob

posts 3

4

This might be a very obvious problem, but I can't seem to find an answer to it and this thread seems to be addressing the differences between int and NSInteger…

I created a class that accepts any object (id).  When I try to pass it a number (like '2') it gives me the following warning:

warning: passing argument # of '…' makes pointer from integer without a cast

Is there a way I should be passing in a number so that it is casted as int/NSInteger?

Thanks in advance.


8:00 pm
November 11, 2008


chrizbo

Noob

posts 3

5

Hi again.

I think I have solved this (along with a similar issue around BOOL) by just using NSNumber.  While it may not be as nice as the int or BOOL types it does still work when referencing it by id…

Thanks.


About the iCodeBlog forum

Currently Online:

3 Guests

Maximum Online: 25

Forums:

Groups: 2

Forums: 6

Topics: 305

Posts: 743

Members:

There are 654 members

There are 1 guests


Brandon has made 87 posts

Top Posters:

bobcubsfan - 53

crazyiez - 30

VertigoSol - 26

Uhu - 17

AdeC - 17

Administrator: Brandon | Moderators: VertigoSol


© - Version 3.1.4 (Build 357)