0

Keep finding similar questions here, but can't find this one

I want to put several objects into an NSMutableArray, and of course be able to retrieve them.

I can put individual strings (First line, Second Line, etc) into the array (as objects), and can see in debugger that they are all being stored - can retrieve them too.

(I don't actually need to put these individual strings in - I only need to put in my "Person" objects. Just thought it might help me to see what does work in terms of moving forward in the NSMutableArray)

Person *aPerson = [[Person alloc] init];        

NSMutableArray *myArray = [[NSMutableArray alloc] init];        
[myArray addObject:@"First line"];        
[myArray addObject:@"Second line"];        
[myArray addObject:@"Third line"];        
[myArray addObject:@"Fourth line"];        
[myArray addObject:@"Fifth line"];        

When I put ONE object into the same array, it goes in fine.

aPerson.firstName = @"Jasper"; // Should be sixth        
aPerson.lastName = @"John";        

[myArray addObject:aPerson];  

In the debugger when stopping at above point I see the "Jasper John" info in a Person object in the NSMutableArray .... But when I put another object into that NSMutableArray, it replaces the last object - it's as those I didn't advance through the NSMutableArray.

aPerson.firstName = @"Izzie";// Should be seventh         
aPerson.lastName = @"TheKitty";        
[myArray addObject:aPerson];        

aPerson.firstName = @"Roxie";        
aPerson.lastName = @"The Pup";        
[myArray addObject:aPerson];        // Should be eighth

When stopping at above point, I just get "Roxie The Pup" for all 3 objects in the NSMutable Array - though my original 5 strings are still filling up the buckets at the beginning.

1
  • BTW 1 - am not doing what the following person did - who is reinitializing his NSMutableArray repeatedly stackoverflow.com/questions/11808720/… 2- I did declare a property for the NSMutableArray I"m using in the .h file associated with this test 3- My test is running inside a standard viewDidLoad method Commented Oct 30, 2015 at 19:42

1 Answer 1

0

Oh got it!

When I was adding my objects, I was really adding a POINTER to the object And since I was using 'aPerson' object, I was simply changing the contents of the same object, as far as the compiler was concerned. So I was updating the contents of that pointer, every time I did an addObject

Person *aPerson2 = [[Person alloc] init];    
aPerson2.firstName = @"Izzie";// Should be seventh
aPerson2.lastName = @"TheKitty";
[myArray addObject:aPerson2];

Person *aPerson3 = [[Person alloc] init];

aPerson3.firstName = @"Roxie";// Should be eightth
aPerson3.lastName = @"The Pup";
[myArray addObject:aPerson3];
Sign up to request clarification or add additional context in comments.

1 Comment

Please note you seem to have (at least) 3 accounts; stackoverflow.com/users/638736/laurel-shimer, stackoverflow.com/users/5508643/laurels and stackoverflow.com/users/430053/laurels. See stackoverflow.com/help/merging-accounts to see how you can merge these accounts (if you want to).

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.