浅拷贝,是对象拷贝的默认的方式。但是,有时候,我们想能够拷贝一份值一样,但是互不影响的对象。那就要用到深拷贝!深拷贝,就是为原对象拷贝一份结构和值相同的克隆对象,原对象与克隆对象占有不同的两块空间。这样,对原对象的修改不会影响到克隆对象;反之,亦然;
归档是指讲对象序列化,如此可以将复杂的对象写入文件中,然后从中读取出他们。只要类中实现的每个属性都是基本数据类型,或者是符合NSCoding协议的某个类的实例,就可以对对象进行归档。
此部分参考:http://blog.sina.com.cn/s/blog_7b9d64af01019k6n.html
实现拷贝,主要用到自己定义的类。因为默认,自己定义的类没有实现NSCopying协议和NSMutableCopying协议。
NSCopying协议: - (id)copyWithZone:(NSZone *)zone;
NSMutableCopying协议: - (id)mutableCopyWithZone:(NSZone *)zone;
分别用来实现不可变对象的拷贝和可变对象的拷贝。
要实现自己类的拷贝,就要实现响应的协议,并且重写协议的响应方法
@interface AddressCard : NSObject<</span>NSCopying,NSMutableCopying>
在*.m中实现协议:
#pragma mark- NSCopying
- (id)copyWithZone:(NSZone *)zone{
AddressCard *newCard=[[self class] allocWithZone:zone];
newCard.name=_name;
newCard.email=_email;
newCard.salary=salary;
return newCard;
}
#pragma mark- NSMutableCopying
- (id)mutableCopyWithZone:(NSZone *)zone{
AddressCard *newCard=[[self class] allocWithZone:zone];
//[newCard setName:_name andEmail:_email andSalary:salary];
newCard.name=_name;
newCard.email=_email;
newCard.salary=salary;
return newCard;
}
使用时:
AddressCard *cloneCard=[card1 copy];
NSCoding是一个可以自己实现的协议,通过扩展你的数据类来支持encode和Decode,目的是为了把数据写进数据缓存,保存到磁盘中。比如
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:name forKey:@"name"];
[aCoder encodeInteger:number forKey:@"number"];
[aCoder encodeObject:array forKey:@"array"];
[aCoder encodeObject:boolValue forKey:@"bool"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
[aDecoder decodeObjectForKey:@"name"];
[aDecoder decodeIntegerForKey:@"number"];
[aDecoder decodeObjectForKey:@"array"];
[aDecoder decodeObjectForKey:@"bool"];
}
return self;
}
为自己开辟一块空的空间,用zone占位
- (id)copyWithZone:(NSZone *)zone
{
//为类自己开辟一块空的空间,用zone占位
CHPerson *person = [[[[self class]allocWithZone:zone] init]autorelease];
//为类的属性开辟一块空的空间,用zone占位
person.name = [[self.name copyWithZone:zone] autorelease];
person.age = [[self.age copyWithZone:zone] autorelease];
return person;
}
//在.h和.m文件中实现
//然后取到document路径
NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//拼接plist的路径
NSString *plistPath = [documentsPath stringByAppendingPathComponent:fileName];
//保存数据
CHPerson *person = [[CHPerson alloc]init];
person.name = self.nameText;
person.age = self.ageText;
person.sex = self.sexText;
//创建NSKeyedArchiver编码对象KeyedArchiver,告诉其装载数据的数据对象data
NSMutableData *data = [[NSMutableData alloc]init];
NSKeyedArchiver *KeyedArchiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
//使用"键-值"对编码,保存希望保存的person 即归档
[KeyedArchiver encodeObject:person forKey:@"Data"];
//结束归档
[KeyedArchiver finishEncoding];
//写入数据到归档文件
[data writeToFile:plistPath atomically:YES];
//归档文件中读数据,创建解码对象
NSData *data = [[NSData alloc]initWithContentsOfFile:plistPath];
NSKeyedUnarchiver *KeyedUnarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
//从解码对象中获得解码后的内容,给对象
CHPerson *person = [KeyedUnarchiver decodeObjectForKey:@"Data"];
//结束解码
[KeyedUnarchiver finishDecoding];
//然后就可以取person的属性了
参考:http://www.cnblogs.com/chenhaosuibi/p/3476263.html