Ios Can';无法写入文档目录
我试图将Ios Can';无法写入文档目录,ios,objective-c,nsfilemanager,Ios,Objective C,Nsfilemanager,我试图将json文件从应用程序包复制到文档目录,但令人惊讶的是,我无法做到这一点。代码如下: NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docDir = [array lastObject]; NSString *path = [docDir stringByAppendingPathExtension:@"themes
json
文件从应用程序包复制到文档
目录,但令人惊讶的是,我无法做到这一点。代码如下:
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [array lastObject];
NSString *path = [docDir stringByAppendingPathExtension:@"themes.json"];
if (![[NSFileManager defaultManager] fileExistsAtPath:path])
{
NSString *themesPath = [[NSBundle mainBundle] pathForResource:@"themes" ofType:@"json"];
NSError *error = nil;
[[NSFileManager defaultManager] copyItemAtPath:themesPath toPath:path error:&error];
if (error)
NSLog(@"Error %@", error);
}
它会产生以下错误:
Error Domain=NSCOCAERRORDOMAIN Code=513“操作无法完成。(Cocoa错误513)。”UserInfo=0x194a0e90{NSSourceFilePathErrorKey=/var/mobile/Applications/ACED3EF9-B0D8-49E8-91DE-37128357E509/Frinder.app/themes.json,NSUserStringVariant=(
复制
),NSFilePath=/var/mobile/Applications/ACED3EF9-B0D8-49E8-91DE-37128357E509/frider.app/themes.json,NSDestinationFilePath=/var/mobile/Applications/ACED3EF9-B0D8-49E8-91DE-37128357E509/Documents.themes.json,NSUnderlyingError=0x194a0400“操作无法完成。不允许操作”}
搜索后,我发现并试图修改我的代码,如下所示:
if (![[NSFileManager defaultManager] fileExistsAtPath:path])
{
NSString *themesPath = [[NSBundle mainBundle] pathForResource:@"themes" ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:themesPath];
BOOL success = [[NSFileManager defaultManager] createFileAtPath:path contents:data attributes:nil];
if (!success)
NSLog(@"Fail");
}
但它也不起作用<代码>成功变量为否
。我最后尝试的是:
[data writeToFile:path atomically:YES];
但还是徒劳。它返回否
我需要注意的是,这个问题只出现在设备上。在模拟器上工作正常。
有人能给我一个线索吗?第一个例子有一点是正确的:
[docDir stringByAppendingPathExtension:@"themes.json"];
应该是:
[docDir stringByAppendingPathComponent:@"themes.json"];
很明显,当您阅读错误消息时,您会看到它试图将文件写入/var/mobile/Applications/ACED3EF9-B0D8-49E8-91DE-37128357E509/Documents.themes.json
。请注意,应该有一个/
的地方有一个/
stringByAppendingPathExtension:
用于向文件添加扩展名,jpg
,txt
,html
stringByAppendingPathComponent
将通过添加正确的目录分隔符将文件添加到路径中,您可以使用大小写/
第二个示例肯定会失败,因为应用程序包是只读的。第一个示例在一个方面是正确的:
[docDir stringByAppendingPathExtension:@"themes.json"];
应该是:
[docDir stringByAppendingPathComponent:@"themes.json"];
很明显,当您阅读错误消息时,您会看到它试图将文件写入/var/mobile/Applications/ACED3EF9-B0D8-49E8-91DE-37128357E509/Documents.themes.json
。请注意,应该有一个/
的地方有一个/
stringByAppendingPathExtension:
用于向文件添加扩展名,jpg
,txt
,html
stringByAppendingPathComponent
将通过添加正确的目录分隔符将文件添加到路径中,您可以使用大小写/
您的第二个示例肯定会失败,因为应用程序包是只读的。@AndreyChernukha抱歉,复制过去的错误,我还添加了一些关于两种方法之间差异的解释。@rckoenes确实有效。谢谢。我将在四分钟内接受你的答复minutes@AndreyChernukha模拟器不那么严格,而且它是模拟器而不是模拟器。模拟器不是一个很好的测试参考。由于它只是将iOS调用转换为OS X。在发送应用程序供审阅之前,请始终在设备上进行测试。@AndreyChernukha很抱歉复制过去的错误,我还添加了一些关于两种方法之间差异的解释。@rckoenes它确实有效。谢谢。我将在四分钟内接受你的答复minutes@AndreyChernukha模拟器不那么严格,而且它是模拟器而不是模拟器。模拟器不是一个很好的测试参考。因为它只是将iOS调用转换为OS X。在发送应用程序供审查之前,请始终在设备上进行测试。