Skip to content

Instantly share code, notes, and snippets.

@sammoore
Created August 3, 2016 19:50
Show Gist options
  • Save sammoore/4349ea87d0d6319e235c00492f2d1163 to your computer and use it in GitHub Desktop.
Save sammoore/4349ea87d0d6319e235c00492f2d1163 to your computer and use it in GitHub Desktop.
A version of NSString's stringByAppendingPathComponent that's readily available on Swift.String. This implementation uses Foundation (hence the Bridged file name). This
import Swift
import Foundation
extension String {
/// WARNING: Unless `paths.isEmpty`, the resultant String is always backed by NSString storage.
init(basePath: String, paths: [String]) {
guard paths.count > 0 else {
self = basePath; return
}
self = paths
.reduce(basePath as NSString, combine: { combined, each in
return combined.stringByAppendingPathComponent(each)
})
as String
}
init(basePath: String, paths: String...) {
self.init(basePath: basePath, paths: paths)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment