In a function, I retrieve a list of directories. I would like to allow the function to accept a filter parameter to filter the retrieval before returning the directories.
This is my function:
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]()
}
}
In the end, I'd like to call the function like this:
getDocumentPaths()
getDocumentPaths { $0.pathExtension == "mp3" }
What is the filter parameter type to allow this?
UPDATE: Based on @phimage's suggestion, I made the above function more Swift 2.0-like. Even more so, FileKit looks great!
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! }
}