将过滤器作为参数传递到Swift函数?
在函数中,我检索目录列表。我想让函数在返回目录之前接受一个过滤参数来过滤检索 这是我的职责:将过滤器作为参数传递到Swift函数?,swift,swift2,closures,Swift,Swift2,Closures,在函数中,我检索目录列表。我想让函数在返回目录之前接受一个过滤参数来过滤检索 这是我的职责: public func getDocumentPaths(filter: ???? = nil) -> [String] { do { var directoryUrls = try NSFileManager.defaultManager().contentsOfDirectoryAtURL(...) // TODO: Filter the direct
public func getDocumentPaths(filter: ???? = nil) -> [String] {
do {
var directoryUrls = try NSFileManager.defaultManager().contentsOfDirectoryAtURL(...)
// TODO: Filter the directory contents if applicable
if filter != nil {
directoryUrls = directoryUrls.filter(filter)
}
return directoryUrls.map { $0.absoluteString }
} catch let error as NSError {
return [String]()
}
}
最后,我想这样调用函数:
getDocumentPaths()
getDocumentPaths { $0.pathExtension == "mp3" }
允许此操作的筛选器参数类型是什么
更新:基于此,我使上述功能更像Swift 2.0。更重要的是,看起来很棒
public func getDocumentPaths() -> [String] {
// Get the directory contents including folders
guard let directoryUrls = try? NSFileManager.defaultManager().contentsOfDirectoryAtURL(NSURL(string:"")!, includingPropertiesForKeys: nil, options: []) else {
// Failed so return empty list
return [String]()
}
return directoryUrls.map { $0.path! }
}
如果要在NSURL上进行筛选
func getDocumentPaths(filter: ((NSURL) -> Bool)? = nil) -> [String] {
do {
var directoryUrls = try NSFileManager.defaultManager().contentsOfDirectoryAtURL(NSURL(string:"")!, includingPropertiesForKeys: nil, options: []) //contentsOfDirectoryAtURL(NSURL(string:"")!)
// Filter the directory contents if applicable
if let f = filter {
directoryUrls = directoryUrls.filter(f)
}
return directoryUrls.map { $0.path }
} catch {
return [String]()
}
}
如果你想过滤绝对字符串
func getDocumentPaths(filter: ((String) -> Bool)? = nil) -> [String] {
do {
var directoryUrls = try NSFileManager.defaultManager().contentsOfDirectoryAtURL...
var directoryStrings = directoryUrls.map { $0.path }
// Filter the directory contents if applicable
if let f = filter {
directoryStrings = directoryStrings.filter(f)
}
return directoryStrings
} catch {
return [String]()
}
}
ps:记录或重新显示异常
ps2:您可以使用类似于库的FileKit轻松管理文件,如果您想在NSURL上进行筛选,可以使用
find
method
func getDocumentPaths(filter: ((NSURL) -> Bool)? = nil) -> [String] {
do {
var directoryUrls = try NSFileManager.defaultManager().contentsOfDirectoryAtURL(NSURL(string:"")!, includingPropertiesForKeys: nil, options: []) //contentsOfDirectoryAtURL(NSURL(string:"")!)
// Filter the directory contents if applicable
if let f = filter {
directoryUrls = directoryUrls.filter(f)
}
return directoryUrls.map { $0.path }
} catch {
return [String]()
}
}
如果你想过滤绝对字符串
func getDocumentPaths(filter: ((String) -> Bool)? = nil) -> [String] {
do {
var directoryUrls = try NSFileManager.defaultManager().contentsOfDirectoryAtURL...
var directoryStrings = directoryUrls.map { $0.path }
// Filter the directory contents if applicable
if let f = filter {
directoryStrings = directoryStrings.filter(f)
}
return directoryStrings
} catch {
return [String]()
}
}
ps:记录或重新显示异常
ps2:您可以使用类似库的FileKit轻松管理文件,有find
method请注意(可能)absoluteString
不是您想要将URL转换为字符串的内容,请改用path
方法。有关类似问题,请参阅。请注意(可能)absoluteString
不是要将URL转换为字符串的内容,请改用path
方法。有关类似问题,请参阅。