Created
May 12, 2016 22:36
-
-
Save algal/1df0ca8cecc97434e7423d265f3e9029 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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