使用Archiver复制对象
#import <Foundation/Foundation.h>
// 使用Archiver复制对象
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableArray *array = [NSMutableArray arrayWithObjects:
[NSMutableString stringWithString:@"abc"],
[NSMutableString stringWithString:@"xyz"],
[NSMutableString stringWithString:@"dbc"],
nil];
NSMutableArray *array1;
NSData *data= [NSKeyedArchiver archivedDataWithRootObject:array];//将数字转换成NAdata
array1 = [NSKeyedUnarchiver unarchiveObjectWithData:data];//将data进行解码,转化成array1
[array addObject:@"小强"];
NSLog(@"array:%@", array);
/*
array:(
abc,
xyz,
dbc,
"\U5c0f\U5f3a"
)
*/
NSLog(@"array1:%@", array1);//这里是将array的数据复制到array1中,所以更改array里面的数据,array1依旧是原始值
/*
array1:(
abc,
xyz,
dbc
)
*/
}
return 0;
}
15-4 使用Archiver复制对象.png
网友评论