我有一个 NSURL 对象。它具有文件系统元素的地址,它可以是文件或目录。我希望能够判断 NSURL 是目录还是文件。
这个我已经试过了,好像不行!
NSURL * temp ....... ;// it is initialized and has a valid value
CFURLRef xx = (CFURLRef)CFBridgingRetain(temp);
if(CFURLHasDirectoryPath(xx)) NSLog(@"was a file");
else NSLog(@"was a folder");
最佳答案
NSNumber *isDirectory;
// this method allows us to get more information about an URL.
// We're passing NSURLIsDirectoryKey as key because that's the info we want to know.
// Also, we pass a reference to isDirectory variable, so it can be modified to have the return value
BOOL success = [url getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:nil];
// If we could read the information and it's indeed a directory
if (success && [isDirectory boolValue]) {
NSLog(@"Congratulations, it's a directory!");
} else {
NSLog(@"It seems it's just a file.");
}
关于objective-c - 如何确定 NSURL 是否是一个目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22277117/