Last active
July 24, 2018 15:54
-
-
Save frozzare/d56f846a58f35779ec26 to your computer and use it in GitHub Desktop.
Example of string replacement in Swift with a dictionary
This file contains 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
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