Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Nsfilemanager 什么';Swift 3枚举目录路径语法的内容是什么?(哈内克)_Nsfilemanager_Swift3_Haneke - Fatal编程技术网

Nsfilemanager 什么';Swift 3枚举目录路径语法的内容是什么?(哈内克)

Nsfilemanager 什么';Swift 3枚举目录路径语法的内容是什么?(哈内克),nsfilemanager,swift3,haneke,Nsfilemanager,Swift3,Haneke,我正在将Haneke缓存框架转换为Swift 3,但我遇到了enumerateContentsOfDirectoryAtPath的问题 以下是原始语法() 问题: Swift 3与enumerateContentsOfDirectoryAtPath的等价物是什么?转换上述示例时应如何使用它?enumerateContentsOfDirectoryAtPath是Haneke框架定义的扩展。这不是NSFileManager的标准方法。您需要首先将该扩展名转换为Swift 3: extension F

我正在将Haneke缓存框架转换为Swift 3,但我遇到了enumerateContentsOfDirectoryAtPath的问题

以下是原始语法()

问题:
Swift 3与enumerateContentsOfDirectoryAtPath的等价物是什么?转换上述示例时应如何使用它?

enumerateContentsOfDirectoryAtPath
是Haneke框架定义的扩展。这不是
NSFileManager
的标准方法。您需要首先将该扩展名转换为Swift 3:

extension FileManager {

    func enumerateContentsOfDirectoryAtPath(_ path: String, orderedByProperty property: URLResourceKey, ascending: Bool, usingBlock block: (URL, Int, inout Bool) -> Void ) {

        let directoryURL = URL(fileURLWithPath: path)
        do {
            let contents = try self.contentsOfDirectory(at: directoryURL, includingPropertiesForKeys: [property.rawValue], options: FileManager.DirectoryEnumerationOptions())
            let sortedContents = contents.sorted(isOrderedBefore: {(URL1: URL, URL2: URL) -> Bool in

                // Maybe there's a better way to do this. See: http://stackoverflow.com/questions/25502914/comparing-anyobject-in-swift

                var value1 : AnyObject?
                do {
                    try (URL1 as NSURL).getResourceValue(&value1, forKey: property);
                } catch {
                    return true
                }
                var value2 : AnyObject?
                do {
                    try (URL2 as NSURL).getResourceValue(&value2, forKey: property);
                } catch {
                    return false
                }

                if let string1 = value1 as? String, let string2 = value2 as? String {
                    return ascending ? string1 < string2 : string2 < string1
                }

                if let date1 = value1 as? Date, let date2 = value2 as? Date {
                    return ascending ? date1 < date2 : date2 < date1
                }

                if let number1 = value1 as? NSNumber, let number2 = value2 as? NSNumber {
                    return ascending ? number1 < number2 : number2 < number1
                }

                return false
            })

            for (i, v) in sortedContents.enumerated() {
                var stop : Bool = false
                block(v, i, &stop)
                if stop { break }
            }

        } catch {
            Log.error("Failed to list directory", error as NSError)
        }
    }

}

func < (lhs: Date, rhs: Date) -> Bool {
    return lhs.compare(rhs) == ComparisonResult.orderedAscending
}

func < (lhs: NSNumber, rhs: NSNumber) -> Bool {
    return lhs.compare(rhs) == ComparisonResult.orderedAscending
}

我迁移到swift 3。github中有一个新的pull请求,可能很有用。
public func enumerator(atPath path: String) -> FileManager.DirectoryEnumerator?
extension FileManager {

    func enumerateContentsOfDirectoryAtPath(_ path: String, orderedByProperty property: URLResourceKey, ascending: Bool, usingBlock block: (URL, Int, inout Bool) -> Void ) {

        let directoryURL = URL(fileURLWithPath: path)
        do {
            let contents = try self.contentsOfDirectory(at: directoryURL, includingPropertiesForKeys: [property.rawValue], options: FileManager.DirectoryEnumerationOptions())
            let sortedContents = contents.sorted(isOrderedBefore: {(URL1: URL, URL2: URL) -> Bool in

                // Maybe there's a better way to do this. See: http://stackoverflow.com/questions/25502914/comparing-anyobject-in-swift

                var value1 : AnyObject?
                do {
                    try (URL1 as NSURL).getResourceValue(&value1, forKey: property);
                } catch {
                    return true
                }
                var value2 : AnyObject?
                do {
                    try (URL2 as NSURL).getResourceValue(&value2, forKey: property);
                } catch {
                    return false
                }

                if let string1 = value1 as? String, let string2 = value2 as? String {
                    return ascending ? string1 < string2 : string2 < string1
                }

                if let date1 = value1 as? Date, let date2 = value2 as? Date {
                    return ascending ? date1 < date2 : date2 < date1
                }

                if let number1 = value1 as? NSNumber, let number2 = value2 as? NSNumber {
                    return ascending ? number1 < number2 : number2 < number1
                }

                return false
            })

            for (i, v) in sortedContents.enumerated() {
                var stop : Bool = false
                block(v, i, &stop)
                if stop { break }
            }

        } catch {
            Log.error("Failed to list directory", error as NSError)
        }
    }

}

func < (lhs: Date, rhs: Date) -> Bool {
    return lhs.compare(rhs) == ComparisonResult.orderedAscending
}

func < (lhs: NSNumber, rhs: NSNumber) -> Bool {
    return lhs.compare(rhs) == ComparisonResult.orderedAscending
}
let fileManager = FileManager.default
let cachePath = self.path

fileManager.enumerateContentsOfDirectoryAtPath(cachePath, orderedByProperty: URLResourceKey.contentModificationDateKey, ascending: true) { (url, _, stop: inout Bool) in
    // ...
}