NSCachesDirectory
是否可能只在设备上读取?我的应用程序的一些用户报告了一些错误,我将其指向此
NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
path = [path stringByAppendingPathComponent:@"VideoCache"];
NSError *error = nil;
[[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
if (error) {
NSLog(@"Error while trying to create directory at path %@: %@", path, error.description);
path = nil;
}
NSString *filePath = [path stringByAppendingPathComponent:@"config.json"];
NSDictionary * json = @{ @"foo" : @"bar"};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:&error];
[jsonData writeToFile:filePath options:NSDataWritingAtomic error:&error];
writeToFile
崩溃,因为path
为零。在caches文件夹中创建目录时,createDirectoryAtPath
在哪些情况下会返回错误?我设法在模拟器上重现它的唯一方法是将应用程序的缓存文件夹设置为只读。
感谢