Argument labels '(stringInterpolationSegment:)' do not match any available overloads

1.9k views Asked by At

After updating the latest Xcode Version 10.2 (10E125), I am getting this error :

Argument labels '(stringInterpolationSegment:)' do not match any available overloads

Could not find any solution yet, any idea please ?

let interpolation1 = String(stringInterpolationSegment:self.addSpotAnnotation!.coordinate.latitude)
let interpolation2 = String(stringInterpolationSegment:self.addSpotAnnotation!.coordinate.longitude)
let coordinate:String = interpolation1 + "," + interpolation2
3

There are 3 answers

0
Mike Taverne On BEST ANSWER

The error is due to changes to how string interpolation works in Swift 5.

The solution is not to replace String(stringInterpolationSegment:) with String(stringInterpolation:):

We do not propose preserving existing init(stringInterpolation:) or init(stringInterpolationSegment:) initializers, since they have always been documented as calls that should not be used directly. [emphasis added]

The example you gave:

coordinate:String = 
      String(stringInterpolationSegment: self.addSpotAnnotation!.coordinate.latitude) 
    + "," 
    + String(stringInterpolationSegment: self.addSpotAnnotation!.coordinate.longitude)

can much more easily be written as:

let coordinate = "\(self.addSpotAnnotation!.coordinate.latitude),\(self.addSpotAnnotation!.coordinate.longitude)"
0
M A Russel On

String Interpolation Updates

Swift 4.2 implements string interpolation by interpolating segments:

let language = "Swift"
let languageSegment = String(stringInterpolationSegment: language)
let space = " "
let spaceSegment = String(stringInterpolationSegment: space)
let version = 4.2
let versionSegment = String(stringInterpolationSegment: version)
let string = String(stringInterpolation: languageSegment, spaceSegment, versionSegment)

In this code, the compiler first wraps each literal segment and then interpolates one with init(stringInterpolationSegment:) . Then, it wraps all segments together with init(stringInterpolation:)

Swift 5 takes a completely different approach

// 1
var interpolation = DefaultStringInterpolation(
literalCapacity: 7,
interpolationCount: 1)
// 2
let language = "Swift"
interpolation.appendLiteral(language)
let space = " "
interpolation.appendLiteral(space)
let version = 5
interpolation.appendInterpolation(version)
// 3
let string = String(stringInterpolation: interpolation)

Here’s what this code does:

Define a DefaultStringInterpolation instance with a certain capacity and interpolation count. Call appendLiteral(:) or appendInterpolation(:) to add literals and interpolated values to interpolation. Produce the final interpolated string by calling init(stringInterpolation:)

credit: raywenderlich

0
Giang On
public func == <T>(lhs: ResultTest<T>, rhs: ResultTest<T>) -> Bool {
    return  "\(lhs)" == "\(rhs)"
}