I’m struggling with this a bit…
I’m creating a new NSMutableArray like so
NSMutableArray *myArray = [NSMutableArray arrayWithObjects:[db getRandomCustomObject], [db GetRandomCustomObject], [db getRandomCustomObject], nil];
The getRandomObject method looks at another array grabs a random object pointer and places it in myArray.
The problem I’m having is that it’s just storing a pointer… so if myArray gets a duplicate randomObject they both have the same memory address location.
I want all objects in myArray to be their own objects… not pointers to the object in another array. I want them to all have their own unique memory location.
I’m thinking maybe in the getRandomCustomObject method I need to do a copyWithZone or something similar before the return?
Can anyone provide me the full sample code to do what I’m attempting? I’ve tried looking at NSCopying protocol and override copyWithZone method for my custom object but I can’t figure out how to do it (still a newb)
Thanks!
Implement
NSCopyingprotocol on your objects and use copy method then inside ofgetRandomCustomObjectmethod.Interface:
and implementation: