NSMutableArray changes to the copy causing changes in the parent NSMutableArray as well

后端 未结 4 556
忘了有多久
忘了有多久 2021-01-24 17:49

I have a NSMutableArray is my delegate that I am using in one of my view controllers as well.

So in viewDidLoad I make a mutable copy of my NSMutableArray like this

4条回答
  •  独厮守ぢ
    2021-01-24 18:17

    I assume you did not implemented the mutableCopyWithZone: correctly.

    You need to implement the NSMutableCopying protocol for the objects you put in the array, this way you could pass a new instance of that object for that case.

    - (id)mutableCopyWithZone:(NSZone *)zone
    {
       YourCustomModel *aCopy = [[[self class] allocWithZone:zone] init];
       if (aCopy) {
         // set properties
    }
       return aCopy
    }
    

提交回复
热议问题