0
What exactly String interpolation in swift means?
I don't understand the formal definition given by SoloLearn , can someone explain me with a example for my low iq brain XD
3 odpowiedzi
+ 4
Basically it's a replacement of a placeholder within a string by the string representation of a variable's content. A placeholder is a part of the string marked by a backslash and followed by an identifier's name wrapped in parentheses.
* Example:
var season = "Summer"
var year = 2020
print("It's \(season) season of \(year)")
WHERE:
\(season) and \(year) are placeholders
season, year are identifiers
The part of the string occupied by placeholders will be replaced by content of respective identifier <season> and <year> .
Output:
It's Summer season of 2020
Just what I learned from a question in forum (cmiiw) ...
String interpolation also works for constants, literals and expressions (Thanks for the note Arsenic).
(Edited)
+ 3
It is a way to creating a string from a mix of constants, variables, literals, and expressions.
Taking Ipang 's example,
When you typed (" it's \(seasons) season of \(year)")
Then entire thing inside the double quotes (" ") is converted to string after filling it with the correct values of placeholders *seasons* and *year*.
+ 2
Thanks for explaination!