开发者

How can i add new object instance in a mutable array using same object reference as the previous one

//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 curre开发者_StackOverflowntPlace 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.


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜