NSFileManager fileExistsAtPath:isDirectory and swift

swift

Solution

The second parameter has the type `UnsafeMutablePointer<ObjCBool>`, which means that you have to pass the address of an `ObjCBool` variable. Example:

var isDir : ObjCBool = false
if fileManager.fileExistsAtPath(fullPath, isDirectory:&isDir) {
    if isDir {
        // file exists and is a directory
    } else {
        // file exists and is not a directory
    }
} else {
    // file does not exist
}

Update for Swift 3 and Swift 4:

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
}

Problem

I'm trying to understand how to use the function `fileExistsAtPath:isDirectory:` with Swift but I get completely lost. This is my code example: ``` var b:CMutablePointer<ObjCBool>? if (fileManager.fileExistsAtPath(fullPath, isDirectory:b! )){ // how can I use the "b" variable?! fileManager.createDirectoryAtURL(dirURL, withIntermediateDirectories: false, attributes: nil, error: nil) } ``` I can't understand how can I access the value for the `b` MutablePointer. What If I want to know if it set to `YES` or `NO`?

Original source