Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Swift fileExistsAtPath(_ path: String, isDirectory isDirectory: UnsafeMutablePointer<ObjCBool>) -> Bool accepts single parameter only

Tags:

ios

swift

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?

like image 875
Georg Tuparev Avatar asked Oct 13 '14 11:10

Georg Tuparev


3 Answers

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 {

}
like image 194
Kirsteins Avatar answered Nov 10 '22 00:11

Kirsteins


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.
}
like image 20
Ian Bytchek Avatar answered Nov 10 '22 02:11

Ian Bytchek


It is

func isDirectory(path: String) -> Bool {
  var isDirectory: ObjCBool = false
  NSFileManager().fileExistsAtPath(path, isDirectory: &isDirectory)
  return Bool(isDirectory)
}
like image 1
onmyway133 Avatar answered Nov 10 '22 01:11

onmyway133