views:

62

answers:

2

I am having problems initalizing an NSArray and adding integers to it here is my code below which I commented in what I am trying to accomplish can someone help me fix it? my app crashes when adding an object, but I dont 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];

                     ticket = [[NSArray alloc] initWithObjects:sltA,sltB,sltC];

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

A: 

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?

Jonathan Grynspan
The code is not correct. `NSArray` objects are immutable.
zneak
And `numberWithInt:` isn't deprecated, so there's no reason to prefer `numberWithInteger:`.
zneak
Oof, overlooked the class of the object.`+numberWithInt:` isn't deprecated, but it's the wrong method for use with an `NSInteger`. `+numberWithInt:` is for `int`s, which are of a different type (by typedef on iPhone and 32-bit Macs, and by actual type on 64-bit Macs.)
Jonathan Grynspan
Also by actual type on iPhone according to my header: `#if __LP64__ || ... || TARGET_OS_IPHONE`
Georg Fritzsche
Jonathan Grynspan
+2  A: 

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.

Conceited Code
... or use `-initWithObjects:`.
Georg Fritzsche
that would only work if the array will never be changed. Maybe he will change slotA before he wants to add it to the array.
Conceited Code
can you give a code example
Nick LaMarca
Actually I only need whats in the array for a second to check for duplicates then I am done with the array until the next iteration...Can you give me an example of initWithObjects? The only problem here is if I do it that way I would be intializing an array, then checking for duplicates, then clearing the array, then on the next iteration reinitializing the array and doing it all over again. Is that ok performance wise? Does that really matter?
Nick LaMarca
NSArray *tmpArray = [NSArray initWithObjects:slotA, slotB, nil];
Conceited Code