归档失败问题出在路径上,NSHomeDirectory()
NSString *stringPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0];
stringPath = [stringPath stringByAppendingPathComponent:@"a.txt"]; // 成功
NSLog(@"1111%@",stringPath);
NSString * path2 = [NSHomeDirectory() stringByAppendingPathComponent:@"a.txt"]; // 失败,注意这里用的是NSHomeDirectory()
NSLog(@"2222%@",path2);
BOOL suc = [NSKeyedArchiver archiveRootObject:@"aaaa" toFile:path2];
if (suc) {
NSLog(@"user 归档成功!");
}else{
NSLog(@"user 归档失败!");
}
1.NSHomeDirectory() :
/var/mobile/Containers/Data/Application/28C6BFE8-4E50-4DEB-9E55-18F0815CDBC5
2.[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0] :
/var/mobile/Containers/Data/Application/F7DC1040-57CB-4104-8AD6-55E9F287FC38/Documents/
看一下,
NSHomeDirectory() + /fileName = /var/mobile/Containers/Data/Application/28C6BFE8-4E50-4DEB-9E55-18F0815CDBC5/fileName 归档失败
[...NSDocumentDirectory...] +/fileName = /var/mobile/Containers/Data/Application/F7DC1040-57CB-4104-8AD6-55E9F287FC38/Documents/fileName 成功
红色区域是问题所在