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

Duke Leto

macrumors regular
Original poster
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?
 
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.
 
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.

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.