开发者

Initializing NSArray and adding an NSInteger to it

开发者 https://www.devze.com 2023-01-06 07:04 出处:网络
I am having problems initalizing an NSArray and adding integers to it. Here is my code, which I commented in what I am trying to accomplish. My app crashes when adding an object, but I don\'t know, if

I am having problems initalizing an NSArray and adding integers to it. Here is my code, which I commented in what I am trying to accomplish. My app crashes when adding an object, but I don't know, if I am clearing the array correctly either.

//CREATE AND INITIALIZE AN ARRAY
NSArray *ticket;                                                        
ticket = [[NSArray alloc] init];

//slotA,slotB,slotC are of type NSInteger that I am trying to add
//to the array (THIS CRASHES)

[ticket addObject:[NSNumber numberWithInt:slotA]];
[ticket addObject:[NSNumber numberWithInt:slotB]];
[ticket addObject:[NSNumber numberWithInt:slotC]];

//I never got to this line of code but I think it has to be wrong
//because this would throw the whole //array away. I dont want it
//to be thrown away I just wanna clear it out but keep it instanciated.

[ticket release];

I tried this,but its saying I am "missing a sentinal function call"

NSArray *ticket;
NSString *sltA=[NSString stringWithFormat:@"%d", slotA];
NSString *sltB=[NSString stringWithFormat:@"%d", slotB];
NSString *sltC=[NSString stringWithFormat:@"%d", slotC开发者_JAVA百科];
ticket = [[NSArray alloc] initWithObjects:sltA,sltB,sltC];

Also, do I have to change the integers to string to put them in an array?


Change NSArray into an NSMutableArray. This will let you add and remove objects.

NSArray is an array that is not suppose to be changed after it is created.

NSMutableArray is just a subclass of NSArray and has many functions that will help you add and remove objects at any point in the array.


The code appears correct (technically you should use +numberWithInteger: instead of +numberWithInt:, but it's certainly not going to cause a crash.) What is the crash you see? Can you post the output from GDB/Xcode?

0

精彩评论

暂无评论...
验证码 换一张
取 消