問題表述:
已經(jīng)有一個數(shù)組A,數(shù)組A內(nèi)有n個Model;此時無論通過copy、mutableCopy、= 賦值出一個新數(shù)組B,修改B中Model的一個屬性,數(shù)組A都會跟著修改,那如何將數(shù)組A與B徹底分開呢?
使用下面方法
self.arrayB = [[NSArray alloc]initWithArray:self.arrayB copyItems:YES];
如何還不行,記得重寫Model內(nèi)- (id)copyWithZone:(NSZone *)zone 方法
- (id)copyWithZone:(NSZone *)zone {
typeof(self) one = [[[self class] allocWithZone:zone] init];
one.屬性 = self.屬性;
return one;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [self init];
[self modelInitWithCoder:aDecoder];
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[self modelEncodeWithCoder:aCoder];
}