0

I need to check the value of a variable returned from a function in Swift with a type of UnsafeMutablePointer?.

Here's the declaration:

    var isDirectory: UnsafeMutablePointer<ObjCBool>?

When I print it out:

    print(isDirectory)

I get:

nil

I am not very well versed in mixing Swift with Objective-C. Any help would be appreciated.

Here is my actual entire code:

    let directoryURL: URL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)

    let pathOldLocalStorage: String = directoryURL.path + "/OldLocalStorage"

    FileManager.default.createFile(atPath: pathOldLocalStorage, contents: nil, attributes: nil)

    var isDirectory: UnsafeMutablePointer<ObjCBool>?

    let fileExists: Bool = FileManager.default.fileExists(atPath: pathOldLocalStorage, isDirectory: isDirectory)

    print("fileExists=", fileExists, "isDirectory=", isDirectory as Any)

Here is the debug window results:

fileExists= true isDirectory= nil

rmaddy
  • 314,917
  • 42
  • 532
  • 579
daniel
  • 1,446
  • 3
  • 29
  • 65

1 Answers1

0

You need to call it like this:

var isDirectory: ObjCBool = false

let fileExists: Bool = FileManager.default.fileExists(atPath: pathOldLocalStorage, isDirectory: &isDirectory)

if fileExists {
    print("isDirectory=\(isDirectory)")
}
OOPer
  • 47,149
  • 6
  • 107
  • 142