【问题标题】:NSFileManager fileExistsAtPath:isDirectory and swiftNSFileManager fileExistsAtPath:isDirectory 和 swift
【发布时间】:2014-09-01 23:30:32
【问题描述】:

我试图了解如何将函数 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,该怎么办?

【问题讨论】:

    标签: swift


    【解决方案1】:

    second parameter 的类型为 UnsafeMutablePointer&lt;ObjCBool&gt;,这意味着 您必须传递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
    }
    

    【讨论】:

    【解决方案2】:

    试图改进其他答案以使其更易于使用。

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

    【讨论】:

    • 我将此答案改写为 URL 的扩展。
    【解决方案3】:

    只是为了超载基地。这是我的,需要一个 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】:

      我刚刚为 FileManager 添加了一个简单的扩展,以使其更整洁。可能有用吗?

      extension FileManager {
          
          func directoryExists(_ atPath: String) -> Bool {
              var isDirectory: ObjCBool = false
              let exists = fileExists(atPath: atPath, isDirectory:&isDirectory)
              return exists && isDirectory.boolValue
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-11-14
        • 1970-01-01
        • 2023-03-21
        • 2015-12-18
        • 2014-12-10
        • 2015-05-29
        • 2015-02-23
        相关资源
        最近更新 更多