this must quite basic, wondering how add integer array?
i know can add strings this:
nsmutablearray *trarray = [[nsmutablearray alloc] init]; [trarray addobject:@"0"]; [trarray addobject:@"1"]; [trarray addobject:@"2"]; [trarray addobject:@"3"];
but guess can't add integers so:
nsmutablearray *trarray = [[nsmutablearray alloc] init]; [trarray addobject:0]; [trarray addobject:1]; [trarray addobject:2]; [trarray addobject:3];
at least compiler isn't happy , tells me i'm doing cast without having told so.
any explanations appreciated.
yes that's right. compiler won't accept code this. difference following:
if write @"a string"
, it's same if created string , autoreleased it. create object using @"a string"
.
but array can store objects (more precise: pointers object). have create objects store integer.
nsnumber *anumber = [nsnumber numberwithinteger:4]; [yourarray addobject:anumber];
to retrive integer again, this
nsnumber anumber = [yourarray objectatindex:6]; int yourinteger = [anumber intvalue];
i hope answer helps understand why doesn't work. can't cast integer pointer. , warning xcode.
edit:
it possible write following
[yourarray addobject:@3];
which shortcut create nsnumber. same syntax available arrays
@[@1, @2];
will give nsarray containing 2 nsnumber objects values 1 , 2.
Comments
Post a Comment