Skip to content

Instantly share code, notes, and snippets.

@algal
Created May 12, 2016 22:36
Show Gist options
  • Save algal/1df0ca8cecc97434e7423d265f3e9029 to your computer and use it in GitHub Desktop.
Save algal/1df0ca8cecc97434e7423d265f3e9029 to your computer and use it in GitHub Desktop.
// known-good: Xcode 7.3.1, Swift 2.2
/**
Filters to only those file URLs whose filenames match the glob pattern.
- parameter fileURLs: file URLs to filter
- parameter filenameGlobPattern: a glob pattern to match against the filename of the file pointed to by a file URL
*/
func URLsWithFilenamesMatchingGlobPattern(fileURLs:[NSURL],filenameGlobPattern:String) -> [NSURL]
{
let filenamePredicate = NSPredicate(format: "SELF like %@", argumentArray: [filenameGlobPattern])
func urlFilenameMatchesGlobPattern(url:NSURL) -> Bool {
guard url.scheme == "file" else { NSLog("%@","passed a non-file URL"); return false }
do {
var filenameObj:AnyObject?
try url.getResourceValue(&filenameObj, forKey: NSURLNameKey)
guard let filename = filenameObj as? NSString else { return false }
return filenamePredicate.evaluateWithObject(filename)
} catch { return false }
}
return fileURLs.filter(urlFilenameMatchesGlobPattern)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment