Подтвердить что ты не робот

NSFileManager fileExistsAtPath: isDirectory и быстрый

Я пытаюсь понять, как использовать функцию fileExistsAtPath:isDirectory: со Swift, но я полностью потеряюсь.

Это мой пример кода:

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)
}

Я не могу понять, как получить доступ к значению для b MutablePointer. Что, если я хочу знать, установлено ли значение YES или NO?

4b9b3361

Ответ 1

Второй параметр имеет тип UnsafeMutablePointer<ObjCBool>, что означает, что вы должны передать адрес переменной ObjCBool. Пример:

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
}

Обновление для Swift 3 и 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
}