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 > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-21-2010, 07:38 AM   #1 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 117
subharb is on a distinguished road
Default Avoiding pointers

Hello guys,

In my code I need to create an object, then pass it to another object that will add it to a NSMutableArray that has as an attribute.
The thing is that the first object will be overwritten, and then added to the second's NSMutableArray.
And here comes the problem, the NSMutableArray instead of having 3 different objects it has 3 objects that are all the same. I believe it's because of the pointers, but is there a way that it doesnt point?

Thanks!
subharb is offline   Reply With Quote
Old 11-21-2010, 09:52 AM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Post your code. It sounds like you're adding the same instance to the array three times; adding it to the array doesn't make a copy, so of course you get the same result three times.

Code:
//how to get three of the same object

MyClass *myObject = [[MyClass alloc] init]];

for(int i=0; i<3; i++){
     myObject.property = i;
     [myArray addObject:myObject];
}

[myObject release];
Now the array contains the same object three times, and "property" is set to "2" for that object.

It sounds like you want this:

Code:
//how to get three different objects

for(int i=0; i<3; i++){
     MyClass *myObject = [[MyClass alloc] init]];
     myObject.property = i;
     [myArray addObject:myObject];
     [myObject release];
}
Now you have three objects with three different properties.

EXTRA CREDIT: If you want the objects to be *nearly* identical with most of the properties the same you could write a copyWithZone method for the class, create one with the base properties set, and then [myObject copy] it before setting the different props and adding to the array. Just don't forget to release it; you own objects that are returned by a correctly written [ copy] method.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 11-21-2010, 10:00 AM   #3 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 117
subharb is on a distinguished road
Default

Great, the alloc solved it.
I supose since it gives another memory address it's virtually another object
So each position of the array points to one memory address with different values.

Thanks!
subharb is offline   Reply With Quote
Old 11-22-2010, 01:51 AM   #4 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Alloc creates a new object and returns the address of the new object. Each object must have a different address; it is not possible for two objects to have the same address.

In your old code you had one object, but several pointers to it. The array has three pointers to the same object. Now you have multiple objects, which is what you need; the array points to three different memory addresses with three different objects.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 01-20-2011, 12:15 PM   #5 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 3
morrismountain is on a distinguished road
Default

Never leave the pointer uninitialized variables - things would not be so bad if uninitialized pointers always contain random values - the vast majority of random values are illegal pointer values, and get a program crash when they are used. The problem is that uninitialized variables tend to take the value of other, previously used pointer variables. These problems are very difficult to debug.
morrismountain is offline   Reply With Quote
Reply

Bookmarks

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: 480
15 members and 465 guests
7twenty7, AlanFloyd, AppsBlogger, David-T, iAppDeveloper, imac74, Jaxen66, lovoyl, Music Man, mutantskin, Sami Gh, SLIC, solardrift, unicornleo, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,683
Threads: 94,131
Posts: 402,932
Top Poster: BrianSlick (7,990)
Welcome to our newest member, unicornleo
Powered by vBadvanced CMPS v3.1.0

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