Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-18-2010, 01:41 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 88
Default NSMutableArray Overwrite

I have the following code snippet:
Code:
//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?
qwertyp is offline   Reply With Quote
Old 01-18-2010, 02:07 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

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.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 01-18-2010, 02:34 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 88
Default

Your assumption was correct. Thanks! Though now if I put the initialization code

(
Code:
NSMutableArray *array = [NSMutableArray arrayWithCapacity: 10];
)

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.
qwertyp is offline   Reply With Quote
Old 01-18-2010, 03:43 PM   #4 (permalink)
Registered Member
 
Join Date: Apr 2009
Posts: 81
Default

NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity: 10];
__________________
iPhone 2G 8Gb 3.1.2 JB, 3G 16Gb 3.1.2 JB, 3GS 16 Gb 3.1.2 JB
MB5,1 C2D 2GHz 2GB RAM
Mac OS X 10.5.8
Jeepston is offline   Reply With Quote
Old 01-18-2010, 03:48 PM   #5 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

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];
__________________

Free Games!
smasher is offline   Reply With Quote
Old 01-18-2010, 05:07 PM   #6 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 88
Default

Quote:
Originally Posted by smasher View Post
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.
qwertyp is offline   Reply With Quote
Old 01-18-2010, 05:59 PM   #7 (permalink)
Registered Member
 
Join Date: Oct 2009
Posts: 155
Default

Quote:
Originally Posted by smasher View Post
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];
I'm having a similar problem.

Last edited by dbarrett; 01-30-2010 at 01:56 AM.
dbarrett is offline   Reply With Quote
Old 01-18-2010, 06:12 PM   #8 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by qwertyp View Post
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
smasher is offline   Reply With Quote
Old 01-19-2010, 07:52 PM   #9 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 6
Default

Quote:
Originally Posted by dbarrett View Post
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.

Last edited by iolas; 01-19-2010 at 07:57 PM.
iolas is offline   Reply With Quote
Old 01-19-2010, 07:56 PM   #10 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 88
Default

Quote:
Originally Posted by smasher View Post
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];
Got it working! Thanks.
qwertyp is offline   Reply With Quote
Reply

Bookmarks

Tags
nsmutablearray, overwrite

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 257
14 members and 243 guests
2WeeksToGo, AdamL, ADY, BrianSlick, Dani77, Dattee, headkaze, mer10, prchn4christ, smithdale87, timle8n1, Touchmint, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,879
Threads: 89,228
Posts: 380,746
Top Poster: BrianSlick (7,129)
Welcome to our newest member, mgon987
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:49 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0