如何使用for循环实例化字典(swift)

时间:2016-07-07 06:45:44

标签: swift

private var selectedButtonArray = [UIButton]() 
private var buttonStepperDictionaryArray = [NSMutableDictionary]()

func appendToButtonStepperDictionaryArray(ixSelectionItem: IxSelectionItem...){
    for iSelectionItem in selectedButtonArray{
        buttonStepperDictionaryArray.append("buttonDesc":iSelectionItem.button.text,"dsds":iSelectionItem.button.tag)
    }
}

结果应如下所示:

[{"buttonDesc":"abc","buttonTag":1},
 {"buttonDesc":"bcd","buttonTag":2},
 {"buttonDesc":"ddd","buttonTag":3}]

我该怎么做?

1 个答案:

答案 0 :(得分:0)

试试这个

private var selectedButtonArray = [UIButton]()
private var buttonStepperDictionaryArray = [NSMutableDictionary]()

func appendToButtonStepperDictionaryArray(ixSelectionItem: IxSelectionItem){
    for iSelectionItem in selectedButtonArray{
        buttonStepperDictionaryArray.append(["buttonDesc": ixSelectionItem.button.text,"dsds":iSelectionItem.tag])
    }
}