//Create mutable array
NSMutableArray *array = [NSMutableArray arrayWithCapacity: 10];
//Add initial objects
[array addObject: @"One"];
[array addObject: @"Two"];
//Insert an object at a particular index
[array insertObject: nameString atIndex: 0];
//Accessing an Array's contents using NSEnumerator
NSEnumerator *enumerator = [array objectEnumerator];
id obj;
while ( obj = [enumerator nextObject] ) {
NSLog( @"%@", obj);
}
nameString is gotten from the contents of a textbox when a button is pushed.
Anyway, what happens is this:
I enter Fred. Terminal prints:
Code:
Fred
One
Two
Good so far, but then I enter George:
Code:
George
One
Two
Why is "George" overwriting "Fred"? I thought the insertObject:atIndex function pushed all values above the index up to make room for the new value. This happens for ONLY the first time I enter a new Name. Why?
I assume this snippet is inside an action that gets called when you click the button? If so, the line you labeled "//Create mutable array" creates a new array each time you click - so when you enter "George" and click, a new array is created. The old array with "Fred" has already been destroyed.
Outside of the buttonClicked method, it won't complie, saying "initializer element is not constant". I have a feeling I'm doing the whole initialization thing wrong.
Code can only be executed inside a method - that's why it's complaining when you try to call [NSMutableArray arrayWithCapacity: 10] outside the method. It can't do that at compile time, only at run time.
You'll have to put that code inside some other method that only gets run once. I assume this code is in some kind of viewController? Look for an initWithNib or viewWillAppear method, and put your array initialization there.
Oh, and you'll have to split up the declaration and the initialization. Right now your variable is local to the method, but if you want to use it from all methods then you need to put it in the .h file, making it an "instance variable".
Code:
//for your .h file, between the interface brackets{}
NSMutableArray* array;
//for your .m file, in initWithNib or viewWillAppear
array = [NSMutableArray arrayWithCapacity: 10];
Code can only be executed inside a method - that's why it's complaining when you try to call [NSMutableArray arrayWithCapacity: 10] outside the method. It can't do that at compile time, only at run time.
You'll have to put that code inside some other method that only gets run once. I assume this code is in some kind of viewController? Look for an initWithNib or viewWillAppear method, and put your array initialization there.
Oh, and you'll have to split up the declaration and the initialization. Right now your variable is local to the method, but if you want to use it from all methods then you need to put it in the .h file, making it an "instance variable".
Code:
//for your .h file, between the interface brackets{}
NSMutableArray* array;
//for your .m file, in initWithNib or viewWillAppear
array = [NSMutableArray arrayWithCapacity: 10];
Well, I did that, and I got no errors, but when the button is pushed the program crashes. I didn't have a viewWillAppear method, so I put the initialization in viewDidLoad.
Code can only be executed inside a method - that's why it's complaining when you try to call [NSMutableArray arrayWithCapacity: 10] outside the method. It can't do that at compile time, only at run time.
You'll have to put that code inside some other method that only gets run once. I assume this code is in some kind of viewController? Look for an initWithNib or viewWillAppear method, and put your array initialization there.
Oh, and you'll have to split up the declaration and the initialization. Right now your variable is local to the method, but if you want to use it from all methods then you need to put it in the .h file, making it an "instance variable".
Code:
//for your .h file, between the interface brackets{}
NSMutableArray* array;
//for your .m file, in initWithNib or viewWillAppear
array = [NSMutableArray arrayWithCapacity: 10];
Well, I did that, and I got no errors, but when the button is pushed the program crashes. I didn't have a viewWillAppear method, so I put the initialization in viewDidLoad.
Sorry, arrayWithCapacity returns an array that gets "autoreleased" - the usual way to deal with this is to create a "property" and use that to retain the object. If you haven't learned about properties yet, try this instead:
Code:
//for your .h file, between the interface brackets{}
NSMutableArray* array;
//for your .m file, in initWithNib or viewWillAppear
array = [[NSMutableArray alloc] init];
__________________
Free Games!
Last edited by smasher; 01-19-2010 at 08:30 PM.
Reason: autorelesed > autoreleased
I'm having a similar problem. I've attached this app so as to explain my issue completely.
Basically I'm trying to add items to a cart. When I page through the app, my selected item is successfully added to the array(NSMutableArray *carCart), but when I leave the page(TheCartViewController) to add another item, the initial item is overwritten by the new added item.
I just tried the suggestions mentioned in this thread but to now avail. Could someone take a gander @ the code and offer a suggestion? I've tried a few things bu nothing seems to work. 'TheCartViewController' class is where the problem lies.
Thanks so much. By the way, this is just a dummy app I use for testing different ideas and concepts.
I am trying to build an app, where someone could store a few data in a table. A very simple one with 4 or 5 cols and as many rows it will be needed. He could be able to add data, store them and at the end of the year export the whole list.
Imagine you have an excel sheet and you want to fill rows and store the data.
How should I start working? Any ideas? I have read a lot especially on the objective-c programming but i cant figure out whether the particular app is complex or easy. I have basic knowledge of c programming.
Sorry, arrayWithCapacity returns an array that gets "autorelesed" - the usual way to deal with this is to create a "property" and use that to retain the object. If you haven't learned about properties yet, try this instead:
Code:
//for your .h file, between the interface brackets{}
NSMutableArray* array;
//for your .m file, in initWithNib or viewWillAppear
array = [[NSMutableArray alloc] init];