Created
May 13, 2016 05:48
-
-
Save algal/6f9db4fc2754287e7de41933da86ea25 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
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