我试图使用repeatedValues初始化程序初始化包含可选值的数组,我惊讶地发现以下代码无法编译 let a: Int?[] = Int?[](count: 10, repeatedValue:nil)// error - Value of Int?[]? not unwrapped; did you mean to use '!'
let a: Int?[] = Int?[](count: 10, repeatedValue:nil) // error - Value of Int?[]? not unwrapped; did you mean to use '!' or '?'?
有趣的是类型签名Int?[] ?,例如一个可选的Int可选数组.这感觉就像一个错误,但也许我对语法缺少了一些东西.我已经查看了一些语言参考但尚未找到答案.
更明确的Array< Int?>类型初始化程序按预期工作
let b: Int?[] = Array<Int?>(count: 10, repeatedValue:nil) // compiles fine
有没有其他人遇到这个并且可以解决一些问题?
编辑
结合非可选类型的额外工作示例来突出显示故障
let c: Int[] = Int[](count: 10, repeatedValue:0) // non-optional shorthand works fine class D { var foo = 1 } let d: D[] = D[](count:10, repeatedValue:D()) // custom class works fine using the shorthand too enum E { case a, b, c, d, e } let e: E[] = E[](count:10, repeatedValue:.e) // enums work too斯威夫特3:
let pageViews = [UIImageView?](repeating: nil, count: pageCount)
斯威夫特2:
let pageViews = [UIImageView?](count: pageCount, repeatedValue: nil)