user6510422 user6510422 - 6 months ago 26
Swift Question

Using stringInterpolationSegment string initializer in swift

While researching the String Structure Reference from Apple (String Structure Reference)

There are initializer methods that accepts Int parameters such as:

init(stringInterpolationSegment expr: Int)

I attempted by writing the code below to learn how to use it plus learn the difference between pass by reference vs. pass by value but can't get it to work using the following:

struct Soho {
var myCountry = "America"
init(stringInterpolationSegment expr: Int){



How should swift code be structured in order to use this string initializer?


From, Apple says:

Creates a string containing the given value’s textual representation.

Do not call this initializer directly. It is used by the compiler when interpreting string interpolations.

(emphasis mine)

And they show you an example in, where we see that indeed we should use String interpolation with "\()".