Cocoa: pointer help

Discussion in 'Mac Programming' started by Duke Leto, May 4, 2008.

  1. Duke Leto macrumors regular

    Mar 17, 2008
    So I have this bit of code that is giving me trouble:

    int i = sqlite3_column_count(select_statement);
    int pk = sqlite3_column_int(select_statement, i-1);
    [pkList addObject:(int *)pk];

    And I get:

    warning: passing argument 1 of 'addObject:' from incompatible pointer type

    on the last line.

    I reviewed memory management and pointers for a good while now (been reading up on various sites for hours), so what am I overlooking?
  2. CaptainZap macrumors regular

    Jan 17, 2007
    NSArrays can only hold objects (At least I think), so in order to store your int, you should use [NSNumber numberWithInt: x], and to recall it [[array objectAtIndex: i] intValue].
    I did the exact thing you did often when I was learning and never understood why I got a warning.
  3. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    Per the NSArray class reference:
    "NSArray and its subclass NSMutableArray manage collections of objects... objects that you add to an array aren’t copied; rather, each object receives a retain message before its id is added to the array.". It looks like objects it is.

    Java, while it seemed silly to me at first, has class wrappers for all of the primitive types (int, short, float, double, char, boolean, byte, long) that are a "capital letter" version that inherit from Object (some by way of Number). This lets you work around things like your example to some extent, because primitives will be auto-wrapped (they call it boxing) in the appropriate class and auto-unwrapped (unboxing) when assigning to primitives, as of version 5.0. Prior you had to box things yourself, but now it's quite transparent and very helpful for making, say, an arraylist of ints.

    I'm sure the autoboxing and unboxing is tough, so I understand that there's nothing like that around in Obj-C, and I wouldn't discourage one from using it, just wanted to toss the concept out there because I think it's pretty cool.

  4. Duke Leto thread starter macrumors regular

    Mar 17, 2008

Share This Page