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

后端 未结 4 557
忘了有多久
忘了有多久 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:28

    That's because the mutable copy of the array is referencing the same objects as mydelegate.array is referencing, so if you change one object property, it's changed in both arrays, as it's the same object.

    You could implement NSCopying protocol in your objects and you can then call initWithArray:copyItems: NSArray method.

    Hope that helps.

提交回复
热议问题