无论如何,是否可以从“createFileAtPath”获取更详细的错误数据,我有点期待 NSError?目前我正在使用 BOOL 返回值。
success = [fileMan createFileAtPath:fileOnDisk contents:dBuffer attributes:nil];
if(success == YES) NSLog(@"FileCreated");
else {
NSLog(@"ERROR: Failed to create file");
return 1;
}
gary
我同意......我很想有一个接受 NSError 的函数!
在这种情况下返回的错误通常是 POSIX 错误中声明的错误之一errno.h
(errno
会自动包含在 Cocoa 或 Foundation 标头中)。
要查看错误,请使用strerror
函数来自errno.h
并参考全球errno
整数,发生问题时由低级 POSIX io 函数设置:
if (![fm createFileAtPath:@"/etc/foobar.txt" contents:data attributes:nil])
{
NSLog(@"Error was code: %d - message: %s", errno, strerror(errno));
}
// output will be: Error was code: 13 - message: Permission denied
错误代码常量列表列在Cocoa 错误处理编程指南 http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ErrorHandlingCocoa/ErrorObjectsDomains/ErrorObjectsDomains.html#//apple_ref/doc/uid/TP40001806-CH202-SW2(除了 errno.h 标头本身)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)