【问题标题】:Why do I keep getting "Extra Argument in Call" for my initializer in Swift为什么我在 Swift 中的初始化程序不断收到“调用中的额外参数”
【发布时间】:2015-02-03 15:20:05
【问题描述】:

对于 init 函数中的重复值,我不断收到错误“调用中的额外参数”。为什么?

 class Point<T> {
/* n dimensional point
 multiline comments ...
*/
let point : [T]
init(dimensions: Int, rValue: Float = 0.0){
    self.point = [T](count: dimensions, repeatedValue:rValue)

}

}

【问题讨论】:

    标签: swift swift-playground generics


    【解决方案1】:

    带有repeatedValue的init的定义是

    Array&lt;T&gt; init(count: Int, repeatedValue: T)

    您的rValue 必须是T 类型

    【讨论】:

      【解决方案2】:

      如果您需要默认值,您的 T 也必须是 FloatLiteralConvertible, 这个:

      Array<T> init(count: Int, repeatedValue: T)
      

      不会这样做。但是,这将起作用并且更有意义,因为您不希望例如“猫”之类的点我猜... 解决方案:

      class Point<T:FloatLiteralConvertible> {
          /* n dimensional point
          multiline comments ...
          */
          let point : [T]
          init(dimensions: Int, rValue: T = 0.0 ){
              self.point = [T](count: dimensions, repeatedValue:rValue)
      
          }
      }
      var pd = Point<Double>(dimensions: 10, rValue: 1.0)
      var pf = Point<Float>(dimensions: 10, rValue: 1.0)
      dump(pd.point)
      dump(pf.point)
      

      【讨论】:

        猜你喜欢
        • 2015-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多