Created
November 22, 2016 20:24
-
-
Save codyrobb/03baa82e9f45b77887b07d879d40c231 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
class Phrase { | |
// MARK: - | |
// MARK: Properties | |
private let template: String | |
private var lookup: [String: String] = [:] | |
// MARK: - | |
// MARK: Initialization | |
init(template: String) { | |
self.template = template | |
} | |
// MARK: - | |
// MARK: Public | |
func put(key: String, value: String) -> Phrase { | |
lookup[key] = value | |
return self | |
} | |
func format() -> String { | |
var formatted = template | |
for (key, value) in lookup { | |
formatted = formatted.replacingOccurrences(of: tokenize(key: key), with: value) | |
} | |
return formatted | |
} | |
// MARK: - | |
// MARK: Private | |
private func tokenize(key: String) -> String { | |
return "{" + key + "}" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Usage: