码迷,mamicode.com
首页 > 编程语言 > 详细

Swift 字典

时间:2017-08-21 14:45:19      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:title   nbsp   move   var   height   fork   str   logs   bsp   

- 字典定义

        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)

 

Swift 字典

标签:title   nbsp   move   var   height   fork   str   logs   bsp   

原文地址:http://www.cnblogs.com/shen5214444887/p/7404000.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!