NSString *rootPath = NSHomeDirectory();
NSString *path = [rootPath stringByAppendingPathComponent:@"test.txt"];
NSLog(@"%@",path);
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
NSArray *array = @[@"one",@"two"];
[archiver encodeObject:array forKey:@"array"];
[archiver finishEncoding];
[data writeToFile:path atomically:YES];
if ([data writeToFile:path atomically:YES]) {
NSLog(@"success");
}else{
NSLog(@"fail");
}
/*
* 取出数据
*/
NSData *datas = [NSData dataWithContentsOfFile:path];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:datas];
NSArray *arrays = [unarchiver decodeObjectForKey:@"array"];
NSLog(@"%@",arrays);
iOS之NSKeyedArchiver的序列化存储
最新推荐文章于 2021-07-09 14:25:09 发布