2

I have an NSMutableArray of custom objects (Subclasses from JSONModel), all the objects are identical. I'm copying a single object as many times I want to add it to the array (If this method is wrong, kindly suggest the proper way). When I edit/modify an object at some index, it tends to modify all the objects in that array.

I made a sample project that explains this issue and uploaded here. Please advice solutions to fix the issue. Thanks!

2
  • add some code here... u can use replaceObjectAtIndex of mutablearray to modify some object in it Commented Feb 11, 2015 at 10:08
  • Add your code where you replace the object Commented Feb 11, 2015 at 10:17

1 Answer 1

3

If it's your _iMember that you're having issues with you are just passing in the same pointer to the object twice, both are referring to the one object. If you want multiple versions of _iMember then you'll need to allocate them, or use copyWithZone (for a shallow copy) depending on the class or structure.

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

4 Comments

If I modify like, Member *_iMember2 = [_iMember copy]... will it fix?
It works.. But I don't understand... I tried the same with NSString... It replaced the first object alone.. Why not here?
How did you create those strings? That will be the reason.
Just NSString *xxx = @"sss"; for (..) {[somearray addObject:xxx];} Today is the day, I learned the importance of 'copy'.. Thanks for that :)

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.