Reputation: 285
So I have found an issue with addObject where instead of appending a value to the end of an array, its just putting in the memory location of my object that I am adding(which i am re-using over and over). The end result is all the values in the array end up being the exact same, which is whatever the last entry was. Obviously, I don't want this.
How can I add a unique object to the array? This is my code to add the object...
[listArray addObject:customer];
listArray is an MSMutable array. Customer is a class with 4 values.
I found this same issue here addObject creates a same value in all array
but that solution gives me an error of 'Thread 1: Program received signal: "SIGABRT".
I create the customer object using the code..
else if ([elementName isEqualToString:@"CustomerListResponse"]) {
NSLog(@"customer element found – create a new instance of Customer class...");
customer = [[CustomerListData alloc] init];
}
Thanks.
Upvotes: 0
Views: 1224
Reputation: 5393
If you are using ARC then try:
CustomerListData *newcustomer = [[CustomerListData alloc] init];
newcustomer = [customer copy];
[listArray addObject:newcustomer];
Upvotes: 1
Reputation: 86661
I'm guessing a bit here because you haven't given us enough code.
If you are doing this:
Customer customer = [[Customer alloc] init];
[customer setProperty1: @"foo1"];
[customer setProperty2: @"foo2"];
[customer setProperty3: @"foo3"];
[customer setProperty4: @"foo4"];
[listArray addObject: customer];
[customer setProperty1: @"bar1"];
[customer setProperty2: @"bar2"];
[customer setProperty3: @"bar3"];
[customer setProperty4: @"bar4"];
[listArray addObject: customer];
[customer release];
you will get the symptoms you describe. customer
is pointing to the same object the second time. You have just changed its properties and the mutable array will now contain the same object in two slots (with the second set of values).
If you do this
Customer customer = [[Customer alloc] init];
[customer setProperty1: @"foo1"];
[customer setProperty2: @"foo2"];
[customer setProperty3: @"foo3"];
[customer setProperty4: @"foo4"];
[listArray addObject: customer];
[customer release];
customer = [[Customer alloc] init];
[customer setProperty1: @"bar1"];
[customer setProperty2: @"bar2"];
[customer setProperty3: @"bar3"];
[customer setProperty4: @"bar4"];
[listArray addObject: customer];
[customer release];
I guarantee you will be getting two different objects with the properties you require in the array. If you think you are doing this, you are wrong. Post the code and we'll tell you where the mistake is.
Upvotes: 2
Reputation: 12195
You first should make your customer object implement the NSCopying protocol. Then, you can do the following:
[listArray addObject:[[customer copy] autorelease]];
This will ensure that a new object is added to the array instead of just a pointer to the existing object that you are constantly reusing.
Note that you have to autorelease this copy as the copy
method returns a new object with a retain count of 1.
Upvotes: 5