Convert Int to NSInteger ??

Discussion in 'Mac Programming' started by Fritzables, Oct 22, 2011.

  1. Fritzables macrumors regular

    Joined:
    May 6, 2011
    Location:
    Brisbane AUSTRALIA
    #1
    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
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    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.
     
  3. Fritzables thread starter macrumors regular

    Joined:
    May 6, 2011
    Location:
    Brisbane AUSTRALIA
    #3
    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
     
  4. Ganesha macrumors regular

    Joined:
    Aug 4, 2009
    #4
    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; 
     

Share This Page