arrays – 在Swift中使用可选值的数组初始化程序简写
发布时间:2020-12-14 04:34:36 所属栏目:百科 来源:网络整理
导读:我试图使用repeatedValues初始化程序初始化包含可选值的数组,我惊讶地发现以下代码无法编译 let a: Int?[] = Int?[](count: 10,repeatedValue:nil)// error - Value of Int?[]? not unwrapped; did you mean to use '!' or '?'? 有趣的是类型签名Int?[] ?,
我试图使用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) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |