我试图使用repeatedValues初始化程序初始化包含可选值的数组,我惊讶地发现以下代码无法编译
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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。