我一直收到错误"呼叫中的额外参数"对于init函数中的repeatedValue。为什么?
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)
}
}
答案 0 :(得分:3)
带有repeatedValue的init的定义是
Array<T> init(count: Int, repeatedValue: T)
您的rValue
必须是T
答案 1 :(得分:0)
如果您需要默认值,您的T也必须是FloatLiteralConvertible, 这样:
Array<T> init(count: Int, repeatedValue: T)
不会这样做。然而,这将起作用并且更有意义,因为你不需要例如#34; Cats&#34;我猜... 解决方案:
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)