将Swift数组转换为包含索引的字典 [英] Convert Swift Array to Dictionary with indexes

查看:685
本文介绍了将Swift数组转换为包含索引的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Xcode 6.4

我有一个UIViews数组,我想用键v0转换为Dictionary, V1 ... 。像这样:

  var dict = [String:UIView]()
for(index,view)in enumerate views){
dict [v \(index)] = view
}
dict // => [v0:< view0>,v1:< view1> ...]

这很有效,但我试图以更实用的方式做到这一点。我想这让我感到困扰,我必须创建 dict 变量。我喜欢这样使用 enumerate() reduce()
$
dict [v \(enumeration.index) ] = enumeration.element //< - 此处为错误
返回字典
}

这感觉更好,但我得到的错误:无法将类型'UIView'的值分配给类型'UIView?'的值我试过了与其他对象 UIView (即: [String] - > [String:String] ),我得到同样的错误。



有什么建议可以清除这个问题吗?

解决方案

尝试像这样:

$ $ p $ reduce(枚举(a),[String:UIView]()){(var dict,枚举) in
dict [\(enumeration.index)] = enumeration.element
return dict
}

Xcode 8•Swift 2.3

 扩展数组元素:AnyObject {
var indexedDictionary:[Int:Element] {
var result:[Int:Element] = [:] $ b $ for(index,element)in enumerate (){
result [index] = element
}
返回结果
}
}

Xcode 8•Swift 3.0

 扩展array {
var indexedDictionary:[Int:Element] {
var result:[Int:Element] = [:]
enumerated()。forEach({result [$ 0.offset] = $ 0 .element})
返回结果
}
}

Xcode 9•Swift 4

 扩展集合{
var indexedDictionary:[Int:Element] {
return enumerated()。reduce(into:[:]){$ 0 [$ 1.offset] = $ 1.element}
}
}


I'm using Xcode 6.4

I have an array of UIViews and I want to convert to a Dictionary with keys "v0", "v1".... Like so:

var dict = [String:UIView]()
for (index, view) in enumerate(views) {
  dict["v\(index)"] = view
}
dict //=> ["v0": <view0>, "v1": <view1> ...]

This works, but I'm trying to do this in a more functional style. I guess it bothers me that I have to create the dict variable. I would love to use enumerate() and reduce() like so:

reduce(enumerate(views), [String:UIView]()) { dict, enumeration in
  dict["v\(enumeration.index)"] = enumeration.element // <- error here
  return dict
}

This feels nicer, but I'm getting the error: Cannot assign a value of type 'UIView' to a value of type 'UIView?' I have tried this with objects other an UIView (ie: [String] -> [String:String]) and I get the same error.

Any suggestions for cleaning this up?

解决方案

try like this:

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: [Int:Element] {
        var result: [Int:Element] = [:]
        for (index, element) in enumerate() {
            result[index] = element
        }
        return result
    }
}

Xcode 8 • Swift 3.0

extension Array  {
    var indexedDictionary: [Int: Element] {
        var result: [Int: Element] = [:]
        enumerated().forEach({ result[$0.offset] = $0.element })
        return result
    }
}

Xcode 9 • Swift 4

extension Collection  {
    var indexedDictionary: [Int: Element] {
        return enumerated().reduce(into: [:]) { $0[$1.offset] = $1.element }
    }
}

这篇关于将Swift数组转换为包含索引的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆