Last active
October 3, 2023 12:23
-
-
Save clarkeben/5c08db681e47b26fed2e68aa15855430 to your computer and use it in GitHub Desktop.
String-interpolation
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
var bobScore = "Bob has a score of \(2 * 3)" |
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
let currentScore = 10 | |
let userScore = "User has a score of \(currentScore)" |
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
struct User { | |
var userName: String | |
var score: Int | |
} | |
extension String.StringInterpolation { | |
mutating func appendInterpolation(user: User) { | |
appendInterpolation("User \(user.userName) has a score of \(user.score)") | |
} | |
} | |
let ben = User(userName: "Ben10", score: 2) | |
print("\(user: ben)") ///User Ben10 has a score of 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment