Testing file existence using NSURL

前端 未结 6 2064
失恋的感觉
失恋的感觉 2020-12-07 20:10

Snow Leopard introduced many new methods to use NSURL objects to refer to files, not pathnames or Core Services\' FSRefs.

However, there\'s one task I can\'t find a

6条回答
  •  轮回少年
    2020-12-07 20:27

    NSURL does have this method:

    - (BOOL)checkResourceIsReachableAndReturnError:(NSError **)error
    

    Which "Returns whether the resource pointed to by a file URL can be reached."

    NSURL *theURL = [NSURL fileURLWithPath:@"/Users/elisevanlooij/nonexistingfile.php" 
                   isDirectory:NO];
    NSError *err;
    if ([theURL checkResourceIsReachableAndReturnError:&err] == NO)
        [[NSAlert alertWithError:err] runModal];
    

提交回复
热议问题