More Detailed Error From createFileAtPath?

前端 未结 2 1045
失恋的感觉
失恋的感觉 2021-02-04 00:53

Is there anyway to get more detailed error data back from \"createFileAtPath\" I was kind of expecting an NSError? Currently I am using the BOOL return value.

su         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-04 01:26

    I agree... I'd love to have a function for this that accepts NSError!

    Errors returned in this case are usually one of the POSIX errors declared in errno.h (errno is automatically included for you as part of the Cocoa or Foundation headers).

    To see the error, use the strerror function from errno.h and reference the global errno integer, which is set by the low-level POSIX io functions when a problem occurs:

    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
    

    The list of error code constants are listed in the in the Error Handling Programming Guide for Cocoa (in addition to the errno.h header itself).

提交回复
热议问题