ane*_*yzm 0 macos cocoa objective-c
我在保存文件的过程中遇到了一个奇怪的问题.writeToFile:options:error:在第一种情况下,我遇到了以下问题:
writeToFile failed with error Error Domain=NSCocoaErrorDomain Code=4 "The file “preferences.plist” doesn’t exist." UserInfo=0xa12c30 {NSFilePath=file:/Users/patrick/Desktop/Untitled.fef/preferences.plist, NSUnderlyingError=0xa0d130 "The operation couldn’t be completed. No such file or directory"}
Run Code Online (Sandbox Code Playgroud)
但该文件实际上存在于该位置.该文件存储在app文档文件包中.
代码:( NSDocument的子类)
NSString *prefFile = [[[self fileURL] absoluteString] stringByAppendingPathComponent:@"preferences.plist"];
NSError *error;
BOOL succes = [[NSKeyedArchiver archivedDataWithRootObject:documentPreferences] writeToFile:prefFile options:0 error:&error];
if (!succes) {
NSLog(@"writeToFile failed with error %@", error);
}
Run Code Online (Sandbox Code Playgroud)
确保在使用...toFile各种类的任何方法时使用实际路径而不是错误的文件URL.您可以通过查看路径来区分它们.如果路径以file://它开头则无法使用.如果它以斜杠开头,/则它是常规文件路径.您仍然可以使用NSFileManager检查文件是否存在.
与您的代码中一样,而不是absoluteString在self.fileURL通话时path.
| 归档时间: |
|
| 查看次数: |
4994 次 |
| 最近记录: |