NSFileManager fileExistsAtPath: isDirectory và nhanh chóng


77

Tôi đang cố gắng hiểu cách sử dụng hàm fileExistsAtPath:isDirectory:với Swift nhưng tôi hoàn toàn bị lạc.

Đây là ví dụ mã của tôi:

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

Tôi không hiểu làm cách nào tôi có thể truy cập giá trị cho bMutablePointer. Điều gì Nếu tôi muốn biết nếu nó được đặt thành YEShoặc NO?

Câu trả lời:


177

Các tham số thứ hai có kiểu UnsafeMutablePointer<ObjCBool>, mà có nghĩa là bạn phải vượt qua địa chỉ của một ObjCBoolbiến. Thí dụ:

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
}

Cập nhật cho Swift 3 và 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
}

1
Đây là liên kết đến tài liệu, developer.apple.com/documentation/foundation/filemanager/…
Metropolis

15
Đây là một trong những giao diện kỳ ​​lạ nhất mà tôi từng thấy. Tại sao không thể chỉ có một hàm isDirectory (đường dẫn :)?
đáng kinh ngạc

12

Đã cố gắng cải thiện câu trả lời khác để dễ sử dụng hơn.

enum Filestatus {
        case isFile
        case isDir
        case isNot
}
extension URL {
    
    
    var filestatus: Filestatus {
        get {
            let filestatus: Filestatus
            var isDir: ObjCBool = false
            if FileManager.default.fileExists(atPath: self.path, isDirectory: &isDir) {
                if isDir.boolValue {
                    // file exists and is a directory
                    filestatus = .isDir
                }
                else {
                    // file exists and is not a directory
                    filestatus = .isFile
                }
            }
            else {
                // file does not exist
                filestatus = .isNot
            }
            return filestatus
        }
    }
}

Tôi đã viết lại câu trả lời này thành một phần mở rộng trên URL.
Jonny

6

Chỉ để quá tải các căn cứ. Đây là của tôi có URL

extension FileManager {

    func directoryExists(atUrl url: URL) -> Bool {
        var isDirectory: ObjCBool = false
        let exists = self.fileExists(atPath: url.path, isDirectory:&isDirectory)
        return exists && isDirectory.boolValue
    }
}

4

Tôi vừa thêm một tiện ích mở rộng đơn giản vào FileManager để làm cho nó gọn gàng hơn một chút. Có thể hữu ích?

extension FileManager {

    func directoryExists(_ atPath: String) -> Bool {
        var isDirectory: ObjCBool = false
        let exists = FileManager.default.fileExists(atPath: atPath, isDirectory:&isDirectory)
        return exists && isDirectory.boolValue
    }
}
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.