【问题标题】:Argument labels '(stringInterpolationSegment:)' do not match any available overloads参数标签 '(stringInterpolationSegment:)' 不匹配任何可用的重载
【发布时间】:2019-03-27 03:39:28
【问题描述】:

更新最新的 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

【问题讨论】:

  • 您能提供发生此错误的代码行吗?
  • 它是一个现有的应用程序,并且有很多地方使用它。其中一个是这样的:let coordinate:String = String(stringInterpolationSegment: self.addSpotAnnotation!.coordinate.latitude) + "," + String(stringInterpolationSegment: self.addSpotAnnotation!.coordinate.longitude)
  • stringInterpolation替换stringInterpolationSegment

标签: swift xcode10.2


【解决方案1】:

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

解决方案String(stringInterpolationSegment:)替换为String(stringInterpolation:)

我们不建议保留现有的 init(stringInterpolation:) 或 init(stringInterpolationSegment:) 初始化程序,因为它们一直被记录为不应直接使用的调用。 [强调]

你给出的例子:

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

可以更容易地写成:

let coordinate = "\(self.addSpotAnnotation!.coordinate.latitude),\(self.addSpotAnnotation!.coordinate.longitude)"

【讨论】:

    【解决方案2】:

    字符串插值更新

    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(:) 将文字和内插值添加到 interpolation。 通过调用 init(stringInterpolation:)

    生成最终的插值字符串

    信用:raywenderlich

    【讨论】:

      【解决方案3】:
      public func == <T>(lhs: ResultTest<T>, rhs: ResultTest<T>) -> Bool {
          return  "\(lhs)" == "\(rhs)"
      }
      

      【讨论】:

        猜你喜欢
        • 2017-08-12
        • 2016-12-16
        • 2018-04-02
        • 2017-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多