zoukankan      html  css  js  c++  java
  • Dictionary的方法总结(创建,修改,遍历......)-Swift

    近日在学习swift,对一些基本数据的用法做了学习总结,本篇主要对swift中的Dictionary的用法进行了总结 :

    //字典
            //创建一个空字典
            var namesOfIntegers = [Int: String]() //键是Int型  值是String型
            // namesOfIntegers 是一个空的 [Int: String] 字典
            namesOfIntegers[16] = "sixteen"// namesOfIntegers 现在包含一个键值对
            //如果上下文已经提供了类型信息,我们可以使用空字典字面量来创建一个空字典,记作[:](中括号中放一个冒号)
            namesOfIntegers = [:]
            // namesOfIntegers 又成为了一个 [Int: String] 类型的空字典
            
            //用字典的字面量创建字典
            // [key 1: value 1, key 2: value 2, key 3: value 3]
            var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]//airports这个字典字面量的任务是构造拥有两个初始数据项的字典,字典被声明为一种[String: String]类型,这意味着这个字典的键和值都是String类型
            
            //和数组一样,我们在用字典字面量构造字典时,如果它的键和值都有各自一致的类型,那么就不必写出字典的类型。 airports字典也可以用这种简短方式定义:
            //var airports = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
            //因为这个语句中所有的键和值都各自拥有相同的数据类型,Swift 可以推断出Dictionary<String, String>是airports字典的正确类型。
            
            //访问和修改字典
            //获取字典的键值对数量
            print("The dictionary of airports contains (airports.count) items.")//打印 The dictionary of airports contains 2 items.
            
            //判断字典是否为空 count是否为0
            if airports.isEmpty{
            
                print("The airports dictionary is empty.")
                
            }else{
            
                print("The airpotes dictionary is not empty.")
            
            }//打印 The airpotes dictionary is not empty.
            
            
            //使用下标语法添加新的数据项
            airports["LHR"] = "London"// airports 字典现在有三个数据项了
            
            //使用下标语法改变特定键对应的值
            airports["LHR"] = "London Heathrow" //LHR对应的值被改成了“London Heathrow”
            
            //作为另一种下标方法,字典的updateValue(_:forKey:)方法可以设置或者更新特定键对应的值。就像上面所示的下标示例,updateValue(_:forKey:)方法在这个键不存在对应值的时候会设置新值或者在存在时更新已存在的值。和上面的下标方法不同的,updateValue(_:forKey:)这个方法返回更新值之前的原值。这样使得我们可以检查更新是否成功,  updateValue(_:forKey:)方法会返回对应值的类型的可选值。
            
            //如果在更新前有值,那返回的这个可选值包含旧值否则是nil
            if let oldValue = airports.updateValue("Dublin Airport", forKey: "DUB") {
                
                //oldValue 返回的就是更新前的旧值,如果更新前键DUB没有对应的值,那此时的返回值oldValue就是nil
                print("The old value for DUB was (oldValue).")// 输出 "The old value for DUB was Dublin."
                
            }
            
            
            //用下标访问键对应的值,因为有可能请求的键没有对应的值存在,字典的下标访问会返回对应值的类型的可选值。如果这个字典包含请求键所对应的值,下标会返回一个包含这个存在值的可选值,否则将返回nil:
            
            if let airportNameDUB = airports["DUB"] {//airportNameDUB是请求访问键DUB对应的可选值
                
                print("The name of the airport is (airportNameDUB).")
                
            } else {
                
                print("That airport is not in the airports dictionary.")
            }
            // 打印 "The name of the airport is Dublin Airport."
            
           
            
            //可以使用下标语法来通过给某个键的对应值 赋值 为 “nil” 来从字典里 移除一个键值对
            airports["APL"] = "Apple Internation"
            // "Apple Internation" 不是真的 APL 机场, 删除它
            airports["APL"] = nil
            // APL 现在被从字典里移除了
            
            //removeValue(forKey:)方法也可以用来在字典中移除键值对。这个方法在键值对存在的情况下会移除该键值对并且返回被移除的值或者在没有值的情况下返回nil
            if let removedValue = airports.removeValue(forKey: "DUB") {
                
                print("The removed airport's name is (removedValue).")
                
            } else {
                
                print("The airports dictionary does not contain a value for DUB.")
                
            }
            //打印 prints "The removed airport's name is Dublin Airport."
            
            
            //字典的遍历,使用for - in 循环来遍历字典中的键值对,每个字典中的数据项都以(key,value)元组的形式返回,可以使用临时常量或者变量来分解这些元组
            for (airportKey,airportName) in airports {
            
                print("字典遍历的结果:(airportKey):(airportName)")
                //打印
                //字典遍历的结果:YYZ:Toronto Pearson
                //字典遍历的结果:LHR:London Heathrow
            }
            
            
            //可以单独遍历出字典里的所有keys 或者是 values值
            
            for airportKey in airports.keys{//获取字典中所有的键
            
                print("airport key:(airportKey)")
                //打印
                //airport key:YYZ
                //airport key:LHR
            }
            
            
            for airportName in airports.values{//获取字典中的所有值
            
                print("airport name:(airportName)")
                //打印
                //airport name:Toronto Pearson
                //airport name:London Heathrow
            }
            
            //当只是需要使用某个字典的键集合或者值集合来作为某个接收Array实例的 API 的参数,可以直接使用keys或者values属性构造一个新数组
            let airportKeyArray = [String](airports.keys)//获取字典里的键构造成新数组“airportKeyArray”
            print("airportAllKeys:(airportKeyArray)")//打印airportAllKeys:["YYZ", "LHR"]
            
            
            let airportNameArray = [String](airports.values)//获取字典里的所有的值构造成新的数组“airportNameArray”
            print("airportAllValues:(airportNameArray)")//打印airportAllValues:["Toronto Pearson", "London Heathrow"]
            
            //Swift 的字典类型是无序集合类型。为了以特定的顺序遍历字典的键或值,可以对字典的keys或values属性使用sorted()方法。
  • 相关阅读:
    初识Python(3)__Python 数字
    初识Python(2)__Python 对象
    正式开始学习python
    现代浏览器的工作原理
    data URI scheme
    连接池
    介绍缓存的基本概念和常用的缓存技术(转)
    前后端及常用语言介绍
    非关系型数据库和关系型数据库区别
    VC编译连接选项详解(转)
  • 原文地址:https://www.cnblogs.com/Rong-Shengcom/p/7081187.html
Copyright © 2011-2022 走看看