Dictionary in Swift with Mutable Array as value is performing very slow? How to optimize or construct properly?

前端 未结 2 504
不知归路
不知归路 2020-11-27 18:30

I am trying to build a data structure in Swift that maps an Integer to an array of objects (a dictionary with int as key and array as value). The objects are extremely small

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-27 19:14

    I had some work arounds until swift 4.2 came along

    var countToColorMap = [Int: [CountedColor]]()
    
    for (color, colorCount) in colorInfo {
        countToColorMap[colorCount, default: [CountedColor]()].append(CountedColor(color: color as! UIColor, colorCount: colorCount))
    }
    

    it is fast and readable

提交回复
热议问题