参数标签“(stringInterpolationSegment:)”不匹配任何可用的重载

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

更新最新的 Xcode 版本 10.2 (10E125) 后,出现此错误:

参数标签“(stringInterpolationSegment:)”不匹配任何可用的重载

还没有找到任何解决方案,请问有什么想法吗?

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

字符串插值更新

Swift 4.2 通过插值段实现字符串插值:

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)

在这段代码中,编译器首先包装每个文字段,然后用 init(stringInterpolationSegment:) 插入一个。然后,它将所有段与 init(stringInterpolation:)

包装在一起

Swift 5 采用完全不同的方法

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

这是这段代码的作用:

定义一个DefaultStringInterpolation实例,具有一定的容量和插值计数。 调用 appendLiteral(:)appendInterpolation(:) 将文字和插值添加到 插值。 通过调用 init(stringInterpolation:)

生成最终的内插字符串

信用:raywenderlich

错误是由于 changes 字符串插值在 Swift 5 中的工作方式造成的。

解决方法是不是String(stringInterpolationSegment:)换成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]

你举的例子:

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

可以更容易地写成:

let coordinate = "\(self.addSpotAnnotation!.coordinate.latitude),\(self.addSpotAnnotation!.coordinate.longitude)"
public func == <T>(lhs: ResultTest<T>, rhs: ResultTest<T>) -> Bool {
    return  "\(lhs)" == "\(rhs)"
}