Skip to content

Instantly share code, notes, and snippets.

@frozzare
Last active July 24, 2018 15:54
Show Gist options
  • Save frozzare/d56f846a58f35779ec26 to your computer and use it in GitHub Desktop.
Save frozzare/d56f846a58f35779ec26 to your computer and use it in GitHub Desktop.
Example of string replacement in Swift with a dictionary
import Foundation
class Template {
class func render (var str: String, dict: Dictionary<String, String>) -> String {
for (key, value) in dict {
str = str.stringByReplacingOccurrencesOfString("{\(key)}", withString: value)
}
return str
}
}
var dict : Dictionary<String, String> = [
"item": "World"
]
Template.render("Hello {item}!", dict: dict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment