createFileAtPath &OCUnit &错误蓝调 [英] createFileAtPath & OCUnit & errno blues
问题描述
I looked here and there but to no avail.
考虑:
- (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
- 为什么 errno 不是 0,即使在第一种情况下(COULD"情况)`success' 是 YES
- 谁能发现在第一个(可以")情况下成功=YES 而在第二个(不能")情况下成功=NO 的实际路径中的差异?
这是在运行 OCUnit 测试时,Xcode 4.6.1运行 iOS 6.1 的模拟器
This is while running a OCUnit test, Xcode 4.6.1 Simulator running iOS 6.1
我只是很困惑:-(
推荐答案
errno
变量通常仅由系统调用(和一些库函数)在调用失败时设置.如果系统调用成功,它不会被修改,并且可能包含来自先前错误的非零值.The
errno
variable is generally only set by system calls (and some library functions) if the call failed. It is not modified if a system call succeeded, and may contain a non-zero value from a previous error.应该在系统调用失败后立即打印或保存
errno
.就你而言,One should print or save
errno
immediately after a failed system call. In your case,NSLog(@"Couldn't write to path: %@", instancePath);
实际上修改了errno.(没有这样的过程"不太可能是正确的失败原因.)
actually modifies errno. ("No such process" is very unlikely to be the correct failure reason.)
这篇关于createFileAtPath &OCUnit &错误蓝调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!