Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Fritzables

macrumors regular
Original poster
May 6, 2011
149
1
Brisbane AUSTRALIA
Now.... I would have thought this was pretty basic but I am having problems bringing a value that has been defined as an int across to a NSInteger.

I see a type casting problem but not sure how to resolve.

How is this done??

Pete
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
Now.... I would have thought this was pretty basic but I am having problems bringing a value that has been defined as an int across to a NSInteger.

I see a type casting problem but not sure how to resolve.

How is this done??

Pete

This is basic C programming. Very, very, very basic C programming.
Look at the header file how NSInteger is defined. You know how to find the definition of a type in XCode?
It is a typedef for a primitive C data type, just like int.
Therefore all you do is a cast like (NSInteger) i.
But in most cases, the compiler will do this automatically because this is just an automatic conversion.

For details, type "N1256" into google which will get you the latest free draft of the C language standard.
 

Fritzables

macrumors regular
Original poster
May 6, 2011
149
1
Brisbane AUSTRALIA
G'Day gnasher729,

Thanks mate.

To give you the story, I have recently come across from MS Win. Under that system I coded in VS C#.

Now I am on a Mac (and loving it) so Cocoa is pretty new to me and a very steep learning curve.

So any guidance is a bonus to me.

I will take a look at Google "N1256" and see what that returns.

Thanks again.

Pete
 

Ganesha

macrumors regular
Aug 4, 2009
111
1
NSInteger is primitive and not an object. Treat it as if it was an int or a long. For example.

Code:
int apple;
NSInteger banana;
    
banana = 1;
apple = banana;    // gives you a compiler warning on 64-bit systems.

If you are worried about the compiler warning, you can use.

Code:
int apple;
NSInteger banana;
    
banana = 1;
apple = (int) banana;

Or

Code:
long apple;
NSInteger banana;
    
banana = 1;
apple = banana;
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.