检查Dictionaries swift 3数组中是否存在值

时间:2017-09-19 05:44:59

标签: swift

我有一个名为arrayOfDict的字典数组,其中包含许多具有相同键的字典对象:QUESTIONIDOPTIONID

[ //arrayOfDict
   {QUESTIONID:1, OPTIONID:0},
   {QUESTIONID:2, OPTIONID:201},
   {QUESTIONID:3, OPTIONID:204)
 ];

只有当我添加的词典不包含相同的arrayOfDict时,我才需要将这些词典添加到QUESTIONID。如果QUESTIONID已经存在,那么相应的OPTIONID需要替换为新的QUESTIONID。如何将QUESTIONIDarrayOfDict中的beforeItemAdd进行比较?

2 个答案:

答案 0 :(得分:5)

你可以尝试获取相同问题的索引,如果你得到它,删除,否则只需插入它,试试这个:

# Without the "&" that cause the process to run in the background
cd ~/work; myapp &> /dev/null; cd -
# Without the " cd -" which returns my to the original directory
cd ~/work; myapp &> /dev/null &

答案 1 :(得分:0)

请检查:

var dict = arrayOfDict.map { dictionary -> ([String : Int]) in
                var dicti = dictionary
                if dicti["QUESTIONID"] == 1 { // You can change 1 with whatever `QUESTIONID` you want to check
                    dicti["OPTIONID"] = 1 // You can change 1 with whatever value you want to assign for `OPTIONID`
                }
                return dicti
            }
print(dict)