我正在使用 Xcode 6.4
我有一个 UIViews 数组,我想用 keys 转换为 Dictionary "v0", "v1"...
。像这样:
var dict = [String:UIView]()
for (index, view) in enumerate(views) {
dict["v\(index)"] = view
}
dict //=> ["v0": <view0>, "v1": <view1> ...]
这行得通,但我正试图以更实用的风格来做到这一点。我想我必须创建dict
变量让我很困扰。我很想使用enumerate()
并reduce()
喜欢这样:
reduce(enumerate(views), [String:UIView]()) { dict, enumeration in
dict["v\(enumeration.index)"] = enumeration.element // <- error here
return dict
}
这感觉更好,但我得到了错误:Cannot assign a value of type 'UIView' to a value of type 'UIView?'
我已经用其他对象UIView
(即:)尝试过这个,[String] -> [String:String]
我得到了同样的错误。
有什么清理这个的建议吗?