Skip to content

Instantly share code, notes, and snippets.

@algal
Created May 13, 2016 05:48
Show Gist options
  • Save algal/6f9db4fc2754287e7de41933da86ea25 to your computer and use it in GitHub Desktop.
Save algal/6f9db4fc2754287e7de41933da86ea25 to your computer and use it in GitHub Desktop.
extension NSURL {
/// Appends path components to the receiver, optionally specifying if full path is a directory to avoid filesystem access
func URLByAppendingPathComponents(pathComponents:[String],lastIsDirectory:Bool? = nil) -> NSURL {
guard pathComponents.isEmpty == false else { return self }
var newURL = self
for idx in pathComponents.startIndex..<pathComponents.endIndex.predecessor() {
newURL = newURL.URLByAppendingPathComponent(pathComponents[idx], isDirectory: true)
}
if let last = pathComponents.last {
if let lastIsDirectory = lastIsDirectory {
newURL = newURL.URLByAppendingPathComponent(last, isDirectory: lastIsDirectory)
}
else {
newURL = newURL.URLByAppendingPathComponent(last)
}
}
return newURL
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment