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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.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 08-19-2011, 01:38 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 14
lynngobin is on a distinguished road
Default Adding object to NSMutableArray changes all objects

One good thing about posting to this forum is that there seems to be a few programmers out there who are tolerant enough to tackle noobie problems like the following.

I'm trying to add objects to a mutable array. I alloc and init the object, and then set some properties:

Code:
Restaurant *restaurant = [[Restaurant alloc] init];
self.currentRestaurantObject = restaurant;
.
.
.
self.currentRestaurantObject.Company = self.currentParsedCharacterData;
.
.
.
self.currentRestaurantObject.SubCatName = self.currentParsedCharacterData;
I then add the object to the array:

Code:
[self.currentParseBatch addObject:self.currentRestaurantObject];
(You might recognize that this code is based on the Apple SeismicXML sample code.)

The problem I've having is that, not only is the object added to the array, but the properties of all the previously added objects get changed to those of the new object.

Can anyone lend a hand?
lynngobin is offline   Reply With Quote
Old 08-19-2011, 02:05 PM   #2 (permalink)
Token farm animal
 
sneaky's Avatar
 
Join Date: Apr 2011
Posts: 321
sneaky is on a distinguished road
Default

The problem here is to do with pointers. When you modify the self.whatever you modify that for everything that is pointing to the same thing.

Consider the following
Code:
for (int i = 0; i < 3; i++) {
   self.stringProperty = [NSString stringWithFormat@"number %i", i];
   [self.myMutableArray addObject:self.stringProperty];
}
If we print the contents of this array it will print
Code:
number 2
number 2
number 2
Why is that? Because when you add self.stringProperty to the array you add a pointer to that object, not a copy of that object. So the next iteration of the loop you go and you change the actual value meaning that the value is changed for everyone that is pointing to the actual value.

Consider instead
Code:
for (int i = 0; i < 3; i++) {
   NSString *localVariableString = [NSString stringWithFormat@"number %i", i];
   [self.myMutableArray addObject:localVariableString];
}
If we print the contents of this array it will print
Code:
number 0
number 1
number 2
Every time we run the loop we create a brand new instance and add that instance to the array rather than re-using the pointer.

There's a fella on here - Brian Slick. He has a really good tutorial on properties in his signature, have a read of that.
sneaky is offline   Reply With Quote
Old 08-21-2011, 11:03 AM   #3 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 14
lynngobin is on a distinguished road
Default

Thanks much, sneaky. I was embarrassed to even ask the question as I knew that the solution was in a concept basic to Objective C. But after a few months away from programming, dang if I could figure it out. Your response pointed me in the right direction and helped solve my problem. I appreciate your help.
lynngobin is offline   Reply With Quote
Reply

Bookmarks

Tags
nsmutablearray

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: 394
13 members and 381 guests
7twenty7, chiataytuday, cristofercolmbos, dedeys78, fiftysixty, gmarro, iOS.Lover, jonathandeknudt, raymng, stanny, tymex, UMAD, xerohuang
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,669
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, dedeys78
Powered by vBadvanced CMPS v3.1.0

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