Problem copying NSMutableArray

后端 未结 2 405
孤独总比滥情好
孤独总比滥情好 2021-01-27 12:08

I am trying to copy one array to another:

NSMutableArray *itemsCopy = [[NSMutableArray alloc] initWithArray:self.items copyItems:YES];

but I ge

相关标签:
2条回答
  • 2021-01-27 12:57

    You need to make sure all the contents of self.items adopt the NSCopying protocol.

    If you just want a shallow copy, send the -mutableCopy message to self.items.

    NSMutableArray *itemsCopy = [self.items mutableCopy];
    
    0 讨论(0)
  • 2021-01-27 13:13

    You have to provide your classes with the copyWithZone selector (according to NSCopying protocol) if you are not copying objects that implement that protocol by default.

    So if you are copying custom objects you have to implement it. The copy method always calls copyWithZone.. and you must always provide the implementation, it can't know what to copy inside objects by itself..

    0 讨论(0)
提交回复
热议问题