将 Swift 数组转换为带索引的字典
Convert Swift Array to Dictionary with indexes
我正在使用 Xcode 6.4
我有一个 UIView 数组,我想将其转换为带有键 "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]
)以外的对象尝试过此操作,但我收到相同的错误.
有什么清理建议吗?
这样试试:
reduce(enumerate(a), [String:UIView]()) { (var dict, enumeration) in
dict["\(enumeration.index)"] = enumeration.element
return dict
}
Xcode 8 • Swift 2.3
extension Array where Element: AnyObject {
var indexedDictionary: [String:Element] {
var result: [String:Element] = [:]
for (index, element) in enumerate() {
result[String(index)] = element
}
return result
}
}
Xcode 8 • Swift 3.0
extension Array {
var indexedDictionary: [String: Element] {
var result: [String: Element] = [:]
enumerated().forEach({ result[String([=12=].offset)] = [=12=].element })
return result
}
}
Xcode 9 - 10 • Swift 4.0 - 4.2
使用Swift4reduce(into:)
方法:
extension Collection {
var indexedDictionary: [String: Element] {
return enumerated().reduce(into: [:]) { [=13=][String(.offset)] = .element }
}
}
使用 Swift 4 Dictionary(uniqueKeysWithValues:)
初始化器并从枚举集合中传递一个新数组:
extension Collection {
var indexedDictionary: [String: Element] {
return Dictionary(uniqueKeysWithValues: enumerated().map{(String([=14=]),)})
}
}
我正在使用 Xcode 6.4
我有一个 UIView 数组,我想将其转换为带有键 "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]
)以外的对象尝试过此操作,但我收到相同的错误.
有什么清理建议吗?
这样试试:
reduce(enumerate(a), [String:UIView]()) { (var dict, enumeration) in
dict["\(enumeration.index)"] = enumeration.element
return dict
}
Xcode 8 • Swift 2.3
extension Array where Element: AnyObject {
var indexedDictionary: [String:Element] {
var result: [String:Element] = [:]
for (index, element) in enumerate() {
result[String(index)] = element
}
return result
}
}
Xcode 8 • Swift 3.0
extension Array {
var indexedDictionary: [String: Element] {
var result: [String: Element] = [:]
enumerated().forEach({ result[String([=12=].offset)] = [=12=].element })
return result
}
}
Xcode 9 - 10 • Swift 4.0 - 4.2
使用Swift4reduce(into:)
方法:
extension Collection {
var indexedDictionary: [String: Element] {
return enumerated().reduce(into: [:]) { [=13=][String(.offset)] = .element }
}
}
使用 Swift 4 Dictionary(uniqueKeysWithValues:)
初始化器并从枚举集合中传递一个新数组:
extension Collection {
var indexedDictionary: [String: Element] {
return Dictionary(uniqueKeysWithValues: enumerated().map{(String([=14=]),)})
}
}