swift: modifying arrays inside dictionaries

后端 未结 5 955
我寻月下人不归
我寻月下人不归 2020-11-30 11:16

How can I easily add elements to an array inside a dictionary? It\'s always complaining with could not find member \'append\' or could not find an overloa

5条回答
  •  甜味超标
    2020-11-30 11:32

    As a simple workaround you can use a NSMutableArray:

    import Foundation
    
    var dict = Dictionary()
    dict["key"] = [1, 2, 3] as NSMutableArray
    dict["key"]!.addObject(4)
    

    I am using effectively such simple solution in my project:

    https://github.com/gui-dos/Guigna/blob/5c02f7e70c8ee3b2265f6916c6cbbe5cd3963fb5/Guigna-Swift/Guigna/GuignaAppDelegate.swift#L1150-L1157

提交回复
热议问题