一般我们用- fileExistsAtPath:
来判断一个文件是否存在,但是如果想同时判断这个路径是否为目录的话可以用- fileExistsAtPath:isDirectory:
,在Swift中如何使用请看下面的代码:
var directory: ObjCBool = ObjCBool(false)
var exists: Bool = NSFileManager.defaultManager().fileExistsAtPath("…", isDirectory: &directory)
if exists && Bool(directory) {
// Exists. Directory.
} else if exists {
// Exists.
}
Swift 3.0 中的写法如下:
let fileManager = FileManager.default
var isDir : ObjCBool = false
if fileManager.fileExists(atPath: fullPath, isDirectory:&isDir) {
if isDir.boolValue {
// file exists and is a directory
} else {
// file exists and is not a directory
}
} else {
// file does not exist
}