一:數(shù)組的copy和mutableCopy:
@interface GYJTest : NSObject
@property(nonatomic,copy)NSString *str;
@property(nonatomic,assign)int age;
@end
@implementation GYJTest
@end
- (void)viewDidLoad{
GYJTest *test = [[GYJTest alloc]init];
test.str = [NSString stringWithFormat:@"%ld",1837891291834223121];
test.age=10;
NSArray*arr2 =@[test];
NSArray*arr3 = arr2.copy;
NSArray*arr4 = arr2.mutableCopy;
NSLog(@"%p",test);
NSLog(@"%p",arr2);
NSLog(@"%p",arr3);
NSLog(@"%p",arr4);
NSLog(@"%p",arr3[0]);
NSLog(@"%p",arr4[0]);
}
打印結(jié)果:
結(jié)論:
1.單純的數(shù)組copy,只是指針復(fù)制,對其中一個(gè)數(shù)組進(jìn)行修改,另一個(gè)也會跟著變動(dòng)
2.數(shù)組的mutableCopy,屬于深復(fù)制,會開辟數(shù)組的堆空間,但是僅限容器的復(fù)制,而容器內(nèi)存儲的指針數(shù)值保持不動(dòng),如果對元素的內(nèi)容進(jìn)行修改,兩個(gè)數(shù)組內(nèi)容都會跟著變動(dòng)
二:那么如果要保證數(shù)組內(nèi)容的完整的復(fù)制,包括數(shù)組元素的深復(fù)制,有兩種方式
A.調(diào)用[[NSMutableArray alloc]initWithArray:arr copyItems:YES]函數(shù)進(jìn)行復(fù)制,該函數(shù)會繼續(xù)調(diào)用元素的NSCopying協(xié)議的- (id)copyWithZone:(nullable NSZone *)zone方法,人為的修改元素的復(fù)制內(nèi)容,以達(dá)到深復(fù)制的目的,元素CCModel的copying協(xié)議按NSMutableCopying方式寫,然后CCModel的嵌套層按正常的NSMutableCopying寫,如下:
@interface CCModel : NSObject<NSCopying>
@property(nonatomic,assign)int count;
@property(nonatomic,strong)Person *per;
@end
@interface Person : NSObject<NSMutableCopying>
@property(nonatomic,copy)NSString *name;
@property(nonatomic,strong)Student *stu;
@end
@interface Student : NSObject<NSMutableCopying>
@property(nonatomic,assign)int score;
@property(nonatomic,copy)NSString *numString;
@end
@implementation CCModel
- (id)copyWithZone:(nullable NSZone *)zone{
CCModel *model = [[CCModel alloc]init];
model.count=self.count;
model.per = self.per.mutableCopy;
return model;
}
@end
@implementation Person
- (id)mutableCopyWithZone:(nullable NSZone *)zone{
Person *model = [[Person alloc]init];
model.name = self.name.mutableCopy;
model.stu = self.stu.mutableCopy;
return model;
}
@end
@implementation Student
- (id)mutableCopyWithZone:(nullable NSZone *)zone{
Student *model = [[Student alloc]init];
model.score=self.score;
model.numString = self.numString.mutableCopy;
return model;
}
@end
驗(yàn)證代碼:
Student *stu = [[Student alloc]init];
stu.score=90;
stu.numString = @"2030221134";
Person *per = [[Person alloc]init];
per.name=@"tom";
per.stu= stu;
CCModel *model = [[CCModel alloc]init];
model.per= per;
model.count=1;
NSArray*arr =@[model];
NSMutableArray *arr1 = [[NSMutableArray alloc]initWithArray:arr copyItems:YES];
NSLog(@"ccmodel:%p",model);
NSLog(@"ccmodel.per:%p",model.per);
NSLog(@"ccmodel.per.stu:%p",model.per.stu);
CCModel*copyModel = arr1.firstObject;
NSLog(@"copyModel:%p",copyModel);
NSLog(@"copyModel.per:%p",copyModel.per);
NSLog(@"copyModel.per.stu:%p",copyModel.per.stu);
打印效果:
從結(jié)果標(biāo)明,通過這種方式可以達(dá)到對元素的深復(fù)制效果,就是需要注意CCModel對象以后可別調(diào)用model.copy,那可是完全的深復(fù)制,完全是兩個(gè)對象了.
B.第二種方式,序列化操作通過實(shí)現(xiàn)NSCoding協(xié)議,調(diào)用序列化方法,深復(fù)制一個(gè)內(nèi)容出來
NSArray*arr =@[model];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:arr];
NSArray *deepCopyArr = [NSKeyedUnarchiver unarchiveObjectWithData:data];
寫法:
@interface CCModel : NSObject<NSCoding>
@property(nonatomic,assign)int count;
@property(nonatomic,strong)Person *per;
@end
@interface Person : NSObject<NSCoding>
@property(nonatomic,copy)NSString *name;
@property(nonatomic,strong)Student *stu;
@end
@interface Student : NSObject<NSCoding>
@property(nonatomic,assign)int score;
@property(nonatomic,copy)NSString *numString;
@end
#import "CCModel.h"
#import <objc/runtime.h>
void encodeWithCoder(NSCoder *coder,id instance){
unsigned int count =0;
Ivar *ivars =class_copyIvarList([instance class], &count);
for(int i = 0; i < count; i++){
Ivar ivar = ivars[i];
const char *cKey = ivar_getName(ivar);
NSString *key = [NSString stringWithCString:cKey encoding:NSUTF8StringEncoding];
id value = [instance valueForKey:key];
[coder encodeObject:value forKey:key];
}
free(ivars);
}
void createInstanceByCoder(NSCoder *coder,id instance){
unsigned int count =0;
Ivar*ivars = class_copyIvarList([instance class], &count);
for(int i = 0; i < count; i++){
Ivar ivar = ivars[i];
const char*cKey = ivar_getName(ivar);
NSString *key = [NSString stringWithCString:cKey encoding:NSUTF8StringEncoding];
id value = [coder decodeObjectForKey:key];
[instance setValue:value forKey:key];
}
free(ivars);
}
@implementation CCModel
- (void)encodeWithCoder:(NSCoder *)coder{
encodeWithCoder(coder,self);
}
- (instancetype)initWithCoder:(NSCoder *)coder{
if (self = [super init]){
createInstanceByCoder(coder,self);
}
return self;
}
@end
@implementation Person
- (void)encodeWithCoder:(NSCoder *)coder{
encodeWithCoder(coder,self);
}
- (instancetype)initWithCoder:(NSCoder *)coder{
if (self = [super init]){
createInstanceByCoder(coder,self);
}
return self;
}
@end
@implementation Student
- (void)encodeWithCoder:(NSCoder *)coder{
encodeWithCoder(coder,self);
}
- (instancetype)initWithCoder:(NSCoder *)coder{
if (self = [super init]){
createInstanceByCoder(coder,self);
}
return self;
}
@end
驗(yàn)證效果
Student *stu = [[Student alloc]init];
stu.score=90;
stu.numString = @"2030221134";
Person *per = [[Person alloc]init];
per.name=@"tom";
per.stu= stu;
CCModel *model = [[CCModel alloc]init];
model.per= per;
model.count=1;
NSArray*arr =@[model];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:arr];
NSArray *deepCopyArr = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(@"ccmodel:%p",model);
NSLog(@"ccmodel.per:%p",model.per);
NSLog(@"ccmodel.per.stu:%p",model.per.stu);
CCModel*copyModel = deepCopyArr.firstObject;
NSLog(@"copyModel:%p",copyModel);
NSLog(@"copyModel.per:%p",copyModel.per);
NSLog(@"copyModel.per.stu:%p",copyModel.per.stu);
打印結(jié)果顯示:數(shù)組元素及元素嵌套的內(nèi)容都不是同一個(gè)對象,達(dá)到了深復(fù)制的目的