Swift 3 enumerateContentsOfDirectoryAtPath 语法是什么? (哈内科)
What's the Swift 3 enumerateContentsOfDirectoryAtPath Syntax? (Haneke)
我正在将 Haneke 缓存框架转换为 Swift 3,并且 运行 遇到了 enumerateContentsOfDirectoryAtPath 的问题。
这是原始语法 (view on github),
let fileManager = NSFileManager.defaultManager()
let cachePath = self.path
fileManager.enumerateContentsOfDirectoryAtPath(cachePath, orderedByProperty: NSURLContentModificationDateKey, ascending: true) { (URL : NSURL, _, inout stop : Bool) -> Void in
if let path = URL.path {
self.removeFileAtPath(path)
stop = self.size <= self.capacity
}
}
我相信我正在寻找的是我通过查看 FileManger 定义找到的以下函数,但是我不知道如何进行转换:
public func enumerator(atPath path: String) -> FileManager.DirectoryEnumerator?
问题
enumerateContentsOfDirectoryAtPath 的 Swift 3 等价物是什么?在转换上述示例时我应该如何使用它?
enumerateContensOfDirectoryAtPath
是Haneke框架定义的扩展available here。这不是 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:
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
// ...
}
我正在将 Haneke 缓存框架转换为 Swift 3,并且 运行 遇到了 enumerateContentsOfDirectoryAtPath 的问题。
这是原始语法 (view on github),
let fileManager = NSFileManager.defaultManager()
let cachePath = self.path
fileManager.enumerateContentsOfDirectoryAtPath(cachePath, orderedByProperty: NSURLContentModificationDateKey, ascending: true) { (URL : NSURL, _, inout stop : Bool) -> Void in
if let path = URL.path {
self.removeFileAtPath(path)
stop = self.size <= self.capacity
}
}
我相信我正在寻找的是我通过查看 FileManger 定义找到的以下函数,但是我不知道如何进行转换:
public func enumerator(atPath path: String) -> FileManager.DirectoryEnumerator?
问题
enumerateContentsOfDirectoryAtPath 的 Swift 3 等价物是什么?在转换上述示例时我应该如何使用它?
enumerateContensOfDirectoryAtPath
是Haneke框架定义的扩展available here。这不是 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:
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
// ...
}