0
//viewController.h file
//---------------------
#import <UIKit/UIKit.h>

@interface ItemClass : NSObject 
{
    NSString* name;
}
@property (nonatomic, retain) NSString* name;
@end



@interface PlaceClass : ItemClass 
{
    NSString* coordinates;
}
@property (nonatomic, retain) NSString* coordinates;
@end




@interface viewController : UIViewController {
    NSMutableArray* placesMutArray;
    PlaceClass* currentPlace;
}

@end

//viewController.m file
//------------------------

#import "viewController.h"
@implementation ItemClass
@synthesize name;
@end


@implementation PlaceClass
@synthesize coordinates;
@end


@implementation viewController

- (void)viewDidLoad {
    [super viewDidLoad];

    placesMutArray = [[NSMutableArray alloc] init];
    currentPlace = [[PlaceClass alloc] init];

    // at some point in code the properties of currentPlace are set
    currentPlace.name = [NSString stringWithFormat:@"abc"];
    currentPlace.coordinates = [NSString stringWithFormat:@"45.25,24.22"];
    // currentPlace added to mutable array
    [placesMutArray addObject:currentPlace];

    //now the properties of currentPlace are changed
    currentPlace.name = [NSString stringWithFormat:@"def"];
    currentPlace.coordinates = [NSString stringWithFormat:@"45.48,75.25"];
    // again currentPlace added to mutable array
    [placesMutArray addObject:currentPlace];

    for(PlaceClass* x in placesMutArray)
    {
        NSLog(@"Name is : %@", x.name);
    }

}
@end

output i get :

Name is : def
Name is : def

desired output :

Name is : abc
Name is : def

I want the placesMutArray to have two separate objects (each allocated separate memory space) each with their own set of "name" and "coordinates" property. But the above code, apparently, just changes the property of same object 'currentPlaces' and its reference gets added to the array twice. Implying i only get one object allocated in memory. When i traverse the array using fast enumeration and NSlog the name property for both elements i jut get last set value twice.

Can adopting NSCopying protocol solve the problem?

[placesMutArray addObject:[currentPlace copy]];

If yes, then how should i go about it? I tried but i am getting lot of errors.

2 Answers 2

0

you're right in that you're using the same instance. You just need to make a new one.

Try this :

// Create a second PlaceClass before setting it's properties to 'def'
currentPlace = [[PlaceClass alloc] init];
currentPlace.name = [NSString stringWithFormat:@"def"];
currentPlace.coordinates = [NSString stringWithFormat:@"45.48,75.25"];

Adding an object to an array doesn't copy the object for you - it just means that the array knows about the object. After you added the first currentPlace to the array your currentPlace variable was still pointing to the first object so when started to set new name and coordinates you were updating the first one, not creating a new one.

Sign up to request clarification or add additional context in comments.

3 Comments

@meggar and @deanWombourne , First of all, thanks for your reply. And, yes, this worked. I had tried this even before going into NSCopying thing and before posting this question here, BUT what i did wrong was that i did alloc..init..to create a new instance at a wrong place in my code (i had put it in parser:foundCaracters method which is called way too frequently and new object instance were created unnecessarily, flushing inserted values in name and coordinates). Your reply made me look in that direction again and i realized what i did wrong. Thanks again for your help
Now you just have to decide which answer to mark as accepted ;) (Annoyingly, @meggar does include the fact that you're leaking memory because you're not calling release - his answer is better!)
I am releasing memory in my actual code, this above was not the whole code, i wrote this in a hurry just to indicate the problem i was facing and missed release statements (thanks for pointing that out @meggar). Btw, in my case i wanted the second PlaceClass object to be referenced using the same name 'currentPlace' (the title says "using the same object reference as the previous one"). So even though both answers had same idea, yours was slightly more helpful.
0

you only created one PlaceClass object (currentPlace), then you added 2 references to this PlaceClass object to your array. You have to create a second object

secondPlace = [[PlaceClass alloc] init];
secondPlace.name = [NSString stringWithFormat:@"def"];
secondPlace.coordinates = [NSString stringWithFormat:@"45.48,75.25"];
[placesMutArray addObject:secondPlace];

or

secondPlace = [currentPlace mutableCopy];
secondPlace.name = [NSString stringWithFormat:@"def"];
secondPlace.coordinates = [NSString stringWithFormat:@"45.48,75.25"];
[placesMutArray addObject:secondPlace];

either way, remember to release the object whenever using alloc, copy or retain

[secondPlace release];

1 Comment

Thanks, it worked!!. Please look at the comment i posted below @deanWombourne 's reply for my feedback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.