I have a class, say the class Person. In than class I have several NSMutableArrays.
@property NSMutableArray *arrayOne;
@property NSMutableArray *arrayTwo;
...
Now I want to copy that Class so I can return it from a function and use the copy and change it’s data. I want to have a new copy of the object in memory not another reference to the same address.
To do that I have implemented this in my Person.m:
-(id)copyWithZone:(NSZone *)zone
{
Person *copy = [[Person allocWithZone:zone] init;
copy.arrayOne = [NSMutableArray arrayWithArray:self.arrayOne];
copy.arrayTwo = [NSMutableArray arrayWithArray:self.arrayTwo];
...
return copy;
}
So far this works just like I want it to, but when I try to sort the arrays of the copy, I get an error:
-[__NSArrayI sortUsingSelector:]: unrecognized selector sent to instance 0x100108a30
I have noticed that the original arrays are of the type ‘_NSArrayM’ but the copy is ‘_NSArrayI’…
So what did I wrong? I have heard of deep copying using NSArchiver and NSUnarchiver… Do I have to use that? Sorry, I am quite new to C and Objective-C… 😀
I hope you can help me out of this. =)
Sorry about my ‘school-english’….
Cheers,
Nick
EDIT: The arrays consist of NSString objects, so I can use
sortUsingSelector:@selector(caseInsensitiveCompare:)
to sort the array.
How are your mutable array properties being declared in your header file?
NB If you’re just doing
then I’m amazed your code has got this far 🙂 The default property memory semantics is
assign– which won’t tell ARC to retain your arrays in any way at all 🙂 You need to specifystrong.