Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Last active December 30, 2015 21:31
Show Gist options
  • Select an option

  • Save kristopherjohnson/46eda25ede3ec9b31e0c to your computer and use it in GitHub Desktop.

Select an option

Save kristopherjohnson/46eda25ede3ec9b31e0c to your computer and use it in GitHub Desktop.
Swift String extension for generating filesystem paths from components
import Foundation
extension String {
/// Returns a new string made by appending to the receiver a filesystem path component.
///
/// - parameter pathComponent: The path component to be appended to the receiver.
///
/// - returns: A new `String` made by appending `pathComponent` to the receiver, preceded if necessary by a path separator.
@warn_unused_result
public func stringByAppendingPathComponent(pathComponent: String) -> String {
return (self as NSString).stringByAppendingPathComponent(pathComponent)
}
/// Return a filesystem path composed of the specified components.
///
/// - parameter pathComponents: list of filesystem path components.
///
/// - returns: A `String` consisting of path components with delimiters between them.
public static func fromPathComponents(pathComponents: String...) -> String {
return pathComponents.reduce("", combine: { $0.stringByAppendingPathComponent($1) })
}
}
@kristopherjohnson
Copy link
Author

Example use:

let plistPath = String.fromPathComponents("/Applications/Xcode.app", "Contents", "version.plist")
// result is "/Applications/Xcode.app/Contents/version.plist"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment