随笔 - 91,  文章 - 0,  评论 - 4,  阅读 - 13万

- 字典定义

        let dict:[String : Any] = ["name": "", "age": 18];
        print(dict)
        
        // [Dictionary<String, Any>]
        let array = [["name": "", "age": 22],
                     ["name": "", "age": 18]]
        print(array)

 

- 增删改

复制代码
        var dict:[String : Any] = ["name": "", "age": 18, "title": "BOSS"]
        print(dict)
        
        // 增加
        dict["height"] = 1.7
        print(dict)
        
        // 修改
        dict["name"] = "老李"
        print(dict)
        
        // 删除
        dict.removeValue(forKey: "title")
复制代码

 

- 遍历

复制代码
        let dict:[String : Any] = ["name": "", "age": 18, "title": "BOSS"]
        for s in dict {
            print(s)
        }
        
        print("----1------")
        
        // let s: (key: String, value: Any)
        for s in dict {
            print("\(s.key)  \(s.value)")
        }
        
        print("------2----")
        for (s, v) in dict {
            print("\(s) -- \(v)")
        }
复制代码

 

- 合并

        var dict1 = ["name": "", "age": 18] as [String : Any]
        let dict2 = ["name": "老李", "title": "BOSS"]
        
        for (k, v) in dict2 {
            dict1[k] = v
        }
        print(dict1)

 

posted on   xiao孛  阅读(607)  评论(0编辑  收藏  举报

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示