查找路径的父目录
答
的NSString
方法-stringByDeletingLastPathComponent
做到了这一点。
您可以使用它像这样:
NSLog(@"%@", [@"/tmp/afolder" stringByDeletingLastPathComponent]);
而且它会记录/tmp
。
答
通常文件的URL类型是NSURL
。现在有一种方法可以用来获取父目录: NSURL *parentDirectory = [fileURL URLByDeletingLastPathComponent];
答
您应该使用URL作为文件位置。如果你有一个String的路径,我会将它转换为URL。对于Swift 3使用
let fileURL: URL = URL(fileURLWithPath: "/path/to/something")
let folderURL = fileURL.deletingLastPathComponent()