我花了几个小时试图解决这个问题,但我已经没想完了. 我要做的就是归档一个对象,但方法archiveRootObject继续返回NO 这是我的代码: NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirecto
我要做的就是归档一个对象,但方法archiveRootObject继续返回NO
这是我的代码:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cacheDirectory = [paths objectAtIndex:0]; cacheDirectory = [cacheDirectory stringByAppendingPathComponent:@"MyAppCache"]; NSString *fullPath = [cacheDirectory stringByAppendingPathComponent:@"archive.data"]; if(![[NSFileManager defaultManager] fileExistsAtPath:fullPath]){ [[NSFileManager defaultManager] createDirectoryAtPath:fullPath withIntermediateDirectories:YES attributes:nil error:nil]; } NSArray *array = [NSArray arrayWithObjects:@"hello", @"world", nil]; NSLog(@"Full Path: %@", fullPath); BOOL res = [NSKeyedArchiver archiveRootObject:array toFile:fullPath]; if(res){ NSLog(@"YES"); }else{ NSLog(@"NO"); }
每次我运行它,它打印NO.
任何帮助,将不胜感激!
您使用路径fullPath创建一个目录,然后尝试在同一路径上写入该文件.用这样的文件覆盖目录是不可能的.使用cacheDirectory字符串创建目录.