首页 > 代码库 > 归档-ios
归档-ios
/****归档**/
NSString *homePath=NSHomeDirectory();
NSString *fileName=@"test.vse";
NSString *path=[homePath stringByAppendingPathComponent:fileName];
NSArray *array=@[@"abc",@"sea",@123];
BOOL success=[NSKeyedArchiver archiveRootObject:array toFile:path];
if (success) {
NSLog(@"success");
}
/**解存档***/
NSArray *newArray=[NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"%@",newArray);
/***第二种存档方式***/
NSString *homePath2=NSHomeDirectory();
NSString *fileName2=@"test2.vse";
NSString *path2=[homePath2 stringByAppendingPathComponent:fileName2];
NSMutableData *data=http://www.mamicode.com/[NSMutableData data];
NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
NSArray *array2=@[@"abc222",@"se22a",@1233];
[archiver encodeInt:100 forKey:@"age"];
[archiver encodeObject:array2 forKey:@"name"];
[archiver finishEncoding];
BOOL success2=[data writeToFile:path2 atomically:YES];
if (success2) {
NSLog(@"success");
}
/***解第二种存档方式***/
NSData *data2=[NSData dataWithContentsOfFile:path2];
NSKeyedUnarchiver *unAchiver=[[NSKeyedUnarchiver alloc] initForReadingWithData:data2];
int age=[unAchiver decodeIntForKey:@"age"];
NSArray *name=[unAchiver decodeObjectForKey:@"name"];
NSLog(@"age=%d,name=%@",age,name);