考虑:
- (void) write: (NSString *) xId data:(NSData *) data forClass: (Class) c {
NSFileManager * fm = [NSFileManager defaultManager] ;
NSString * instancePath = [self instancePath:xId forClass: c] ;
errno = 0 ;
BOOL success = [fm createFileAtPath: instancePath
contents: data
attributes: nil] ;
if (!success) {
::NSLog(@"Couldn't write to path: %@", instancePath) ;
::NSLog(@"Error was code: %d - message: %s", errno, strerror(errno));
} else {
::NSLog(@"COULD write to path: %@", instancePath) ;
::NSLog(@"Error was code: %d - message: %s", errno, strerror(errno));
}
}然后打印:
2013-03-22 18:59:27.177 otest[18490:303] COULD write to path: /Users/verec/Library/Application Support/iPhone Simulator/6.1/Documents/cal/ModelRepo/ModelRepo#0.sexp
2013-03-22 18:59:27.177 otest[18490:303] Error was code: 3 - message: No such process
2013-03-22 18:59:27.178 otest[18490:303] Couldn't write to path: /Users/verec/Library/Application Support/iPhone Simulator/6.1/Documents/cal/ModelContainer/20130322.sexp
2013-03-22 18:59:27.178 otest[18490:303] Error was code: 3 - message: No such process这是在运行OCUnit测试时,运行iOS 6.1的Xcode 4.6.1模拟器
我只是困惑:-
发布于 2013-03-22 19:37:23
errno变量。如果系统调用成功,则不会对其进行修改,并且可能包含来自先前错误的非零值。errno。在你的情况下
NSLog(“无法写入路径:%@",instancePath);
实际上修改了errno。(“没有这样的过程”不太可能是正确的失败原因。)errno在createFileAtPath失败后包含正确的值。它实际上是在我的测试中做的,但是没有文档证明这个方法正确地设置/保存了errno。发布于 2015-01-06 19:15:47
我编写这个答案是为了用代码blob更详细地解决verec的错误代码。实际上,他在对已被接受的答案的评论中提到了这一点。
他从createFileAtPath那里得到的错误是3 (ESRCH) - No Such Process
原因之一可能是createFileAtPath不会创建中间目录,所以如果您想要创建的路径上的目录不存在,那么这个错误代码就会失败。
相反,您必须使用createDirectoryAtPath:withIntermediateDirectories:attributes:error:首先创建目录,然后在成功创建目录之后使用createFileAtPath。
NSString *fileParentFolderPath;
NSString *filePath;
//first create the directory, createFileAtPath can't create intermediate dirs
NSError *error;
if([[NSFileManager defaultManager] createDirectoryAtPath:fileParentFolderPath
withIntermediateDirectories:YES attributes:nil error:&error]) {
//then create the file
if(![[NSFileManager defaultManager] createFileAtPath:filePath
contents:nil attributes:nil]) {
NSLog(@"Faliure creating File error was code: %d - message: %s", errno, strerror(errno));
};
} else {
NSLog(@"Faliure creating dir w error: %@", error);
};https://stackoverflow.com/questions/15578179
复制相似问题