The method fileExistsAtPath() in the example below accept single argument only.
if fm.fileExistsAtPath(result, isDirectory:&isDir) {
The exact error message is: "Extra argument 'isDirectory' in call".
Any idea what's wrong?
The method fileExistsAtPath() in the example below accept single argument only.
if fm.fileExistsAtPath(result, isDirectory:&isDir) {
The exact error message is: "Extra argument 'isDirectory' in call".
Any idea what's wrong?
The problem is that isDirectory
is UnsafeMutablePointer<ObjCBool>
and not UnsafeMutablePointer<Bool>
you provide. You can use the following code:
var isDir = ObjCBool(false)
if NSFileManager.defaultManager().fileExistsAtPath("", isDirectory: &isDir) {
}
if isDir.boolValue {
}
Some might find this a little neater. This is Swift 3.
var directory: ObjCBool = ObjCBool(false)
var exists: Bool = FileManager.default.fileExists(atPath: "…", isDirectory: &directory)
if exists && directory.boolValue {
// Exists. Directory.
} else if exists {
// Exists.
}
It is
func isDirectory(path: String) -> Bool {
var isDirectory: ObjCBool = false
NSFileManager().fileExistsAtPath(path, isDirectory: &isDirectory)
return Bool(isDirectory)
}
In Swift3
var isDirectory:ObjCBool = true
var exists = FileManager.default.fileExists(atPath: cachePath, isDirectory: &isDirectory)
You can use the following code as an extension . to check if a directory exists or not in Swift 4.0
import Foundation
extension FileManager {
func directoryExists (atPath path: String) -> Bool {
var directoryExists = ObjCBool.init(false)
let fileExists = FileManager.default.fileExists(atPath: path, isDirectory: &directoryExists)
return fileExists && directoryExists.boolValue
}
}
This is Swift 5.3.
var directoryExists: ObjCBool = ObjCBool(false)
let fileExists = FileManager.default.fileExists(atPath: path, isDirectory: UnsafeMutablePointer<ObjCBool>(mutating: withUnsafePointer(to: &directoryExists){ $0 }))
It seems like not allowed to check file and directory in the same time.