user6510422 user6510422 - 4 months ago 22
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?

Answer

From https://developer.apple.com/reference/swift/string/1539185-init, 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 https://developer.apple.com/reference/swift/stringinterpolationconvertible, where we see that indeed we should use String interpolation with "\()".