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

十年热恋 提交于 2019-12-13 19:34:14

问题


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

回答1:


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)"



回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/55369435/argument-labels-stringinterpolationsegment-do-not-match-any-available-overl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!