美文网首页iOS 开发小技巧
Swift 3 学习 - 04 集合类型

Swift 3 学习 - 04 集合类型

作者: iceMaple | 来源:发表于2016-10-14 14:44 被阅读51次
    Swift 3.png
    Swift 语言提供Arrays、Sets和Dictionaries三种基本的集合类型用来存储集合数据。数组(Arrays)是有序数据的集。集合(Sets)是无序无重复数据的集。字典(Dictionaries)是无序的键值对的集。
    CollectionTypes_intro_2x.png

    1 .数组 Arrays

    数组使用有序列表存储同一类型的多个值。相同的值可以多次出现在一个数组的不同位置中。这和OC中的类似。

    • 1.1 数组的简单语法

    建立Swift 数组应该遵循像Array<Element>这样的形式,其中Element是这个数组中唯一允许存在的数据类型。我们也可以使用像[Element]这样的简单语法。尽管两种形式在功能上是一样的,但是推荐较短的那种,而且在本文中都会使用这种形式来使用数组。

    • 1.2 创建一个空数组、

    我们可以使用构造语法来创建一个由特定数据类型构成的空数组,就像上面说的,[Element]这样的简单语法

    var someInts = [Int]()
    print("someInts is of type [Int] with \\(someInts.count) items.")
    // Prints "someInts is of type [Int] with 0 items."
    
    1.2.png
    如上图,通过构造函数的类型,someInts的值类型被推断为[Int]
    .
    当然你不指定数据类型也是可以建立的,为[Any]类型,
    1.2.2.png
    如果代码上下文中已经提供了类型信息,例如一个函数参数或者一个已经定义好类型的常量或者变量,我们可以使用空数组语句创建一个空数组,它的写法很简单:[]
    someInts.append(3)
    // someInts now contains 1 value of type Int
    someInts = []
    // someInts is now an empty array, but is still of type [Int]
    

    注意:这种方式someInts = []新建的数组保持原来的类型。

    • 1.3 .创建一个带有默认值的数组

    Swift 中的Array类型还提供一个可以创建特定大小并且所有数据都被默认的构造方法。我们可以把准备加入新数组的适当类型的初始值(repeatedValue)和数组数据项的数量(count)传入数组的构造函数

    var threeDoubles = Array(repeating: 0.0, count: 3)
    // threeDoubles is of type [Double], and equals [0.0, 0.0, 0.0]
    
    • 1.4 通过两个数组相加创建一个数组

    我们可以使用加法操作符(+)来组合两种已存在的相同类型数组。新数组的数据类型会被从两个数组的数据类型中推断出来

    var threeDoubles = Array(repeating: 0.0, count: 3)
    // threeDoubles is of type [Double], and equals [0.0, 0.0, 0.0]
    var anotherThreeDoubles = Array(repeating: 2.5, count: 3)
    // anotherThreeDoubles is of type [Double], and equals [2.5, 2.5, 2.5]
    var sixDoubles = threeDoubles + anotherThreeDoubles
    // sixDoubles is inferred as [Double], and equals [0.0, 0.0, 0.0, 2.5, 2.5, 2.5]
    
    • 1.5 用数组文本直接构造数组

    我们可以使用数组文本来进行数组构造,这是一种用一个或者多个数值构造数组的简单方法。数组文本是一系列由逗号分割并由方括号包含的数值:[value 1, value 2, value 3]
    下面的示例创建一个名为shoppingList的数组存储String值:

    var shoppingList: [String] = ["Eggs", "Milk"]
    // shoppingList has been initialized with two initial items
    

    shoppingList变量被声明为“字符串值类型的数组“,记作[String]。 因为这个数组被规定只有String一种数据结构,所以只有String类型可以在其中被存取。 在这里,shoppinglist数组由两个String值("Eggs" 和"Milk")构造,并且由字面量定义。
    由于 Swift 的类型推断机制,当我们用字面量构造只拥有相同类型值数组的时候,我们不必把数组的类型定义清楚。 shoppinglist的构造也可以这样写:var shoppingList = ["Eggs", "Milk"]
    因为所有字面量中的值都是相同的类型,Swift 可以推断出[String]shoppinglist中变量的正确类型。

    • 1.6 访问和修改数组

    我们可以通过数组的方法和属性来访问和修改数组,或者使用下标语法。
    (1):可以使用数组的只读属性count来获取数组中的数据项数量:

    var shoppingList: [String] = ["Eggs", "Milk"]
    print("The shoppingList contains \\(shoppingList.count) items.")
    // Prints "The shoppingList contains 2 items.
    

    (2):使用布尔值属性isEmpty作为检查count属性的值是否为0

    var shoppingList: [String] = ["Eggs", "Milk"]
    if shoppingList.isEmpty {
    print("The shopping list is empty.")
    } else {
    print("The shopping list is not empty.")
    }
    // Prints "The shopping list is not empty."
    

    (3):也可以使用append(_:)方法在数组后面添加新的数据项:

    var shoppingList: [String] = ["Eggs", "Milk"]
    shoppingList.append("Flour")
    // shoppingList now contains 3 items, and someone is making pancakes
    

    (4):使用加法赋值运算符(+=)也可以直接在数组后面添加一个或多个拥有相同类型的数据项:

    var shoppingList: [String] = ["Eggs", "Milk"]
    shoppingList.append("Flour")
    // shoppingList now contains 3 items, and someone is making pancakes
    shoppingList += ["Baking Powder"]
    // shoppingList now contains 4 items
    shoppingList += ["Chocolate Spread", "Cheese", "Butter"]
    // shoppingList now contains 7 items
    

    (5):可以直接使用下标语法来获取数组中的数据项,把我们需要的数据项的索引值直接放在数组名称的方括号中,和OC一样索引从0开始:

    var firstItem = shoppingList[0]
    // firstItem is equal to "Eggs"
    

    (6):我们也可以用下标来改变某个已有索引值对应的数据值:

    var shoppingList: [String] = ["Eggs", "Milk"]
    shoppingList[0] = "Six eggs"
    // the first item in the list is now equal to "Six eggs" rather than "Eggs"
    

    (7):还可以利用下标来一次改变一系列数据值,即使新数据和原有数据的数量是不一样的。下面的例子把"Chocolate Spread","Cheese",和"Butter"替换为"Bananas"和 "Apples":

    var shoppingList: [String] = ["Eggs", "Milk"]
    shoppingList.append("Flour")
    // shoppingList now contains 3 items, and someone is making pancakes
    shoppingList += ["Baking Powder"]
    // shoppingList now contains 4 items
    shoppingList += ["Chocolate Spread", "Cheese", "Butter"]
    // shoppingList now contains 7 items
    shoppingList[4...6] = ["Bananas", "Apples"]
    // shoppingList now contains 6 items
    

    (8):调用数组的insert(_:atIndex:)方法来在某个具体索引值之前添加数据项:

    var shoppingList: [String] = ["Eggs", "Milk"]
    shoppingList.append("Flour")
    // shoppingList now contains 3 items, and someone is making pancakes
    shoppingList += ["Baking Powder"]
    // shoppingList now contains 4 items
    shoppingList += ["Chocolate Spread", "Cheese", "Butter"]
    // shoppingList now contains 7 items
    shoppingList[4...6] = ["Bananas", "Apples"]
    // shoppingList now contains 6 items
    shoppingList.insert("Maple Syrup", at: 0)
    // shoppingList now contains 7 items
    // "Maple Syrup" is now the first item in the list
    

    这次insert(_:atIndex:)方法调用把值为"Maple Syrup"的新数据项插入列表的最开始位置,并且使用0作为索引值
    (9):我们可以使用removeAtIndex(_:)方法来移除数组中的某一项

    这个方法把数组在特定索引值中存储的数据项移除并且返回这个被移除的数据项(我们不需要的时候就可以无视它):

    var shoppingList: [String] = ["Eggs", "Milk"]
    shoppingList.append("Flour")
    // shoppingList now contains 3 items, and someone is making pancakes
    shoppingList += ["Baking Powder"]
    // shoppingList now contains 4 items
    shoppingList += ["Chocolate Spread", "Cheese", "Butter"]
    // shoppingList now contains 7 items
    shoppingList[4...6] = ["Bananas", "Apples"]
    // shoppingList now contains 6 items
    shoppingList.insert("Maple Syrup", at: 0)
    // shoppingList now contains 7 items
    // "Maple Syrup" is now the first item in the list
    let mapleSyrup = shoppingList.remove(at: 0)
    // the item that was at index 0 has just been removed
    // shoppingList now contains 6 items, and no Maple Syrup
    // the mapleSyrup constant is now equal to the removed "Maple Syrup" string
    

    当我们删除数组最后一项时shoppingList.remove(at: shoppingList.count-1),但可以更便捷的使用 removeLast()

    let apples = shoppingList.removeLast()
    // the last item in the array has just been removed
    // shoppingList now contains 5 items, and no apples
    // the apples constant is now equal to the removed "Apples" string
    

    (10):数组的遍历

    我们可以使用for-in循环来遍历所有数组中的数据项:

    for item in shoppingList {
    print(item)
    }
    // Six eggs
    // Milk
    // Flour
    // Baking Powder
    // Bananas
    

    如果我们同时需要每个数据项的值和索引值,可以使用enumerate()方法来进行数组遍历。enumerate()返回一个由每一个数据项索引值和数据值组成的元组。我们可以把这个元组分解成临时常量或者变量来进行遍历:

    for (index, value) in shoppingList.enumerated() {
    print("Item \\(index + 1): \\(value)")
    }
    // Item 1: Six eggs
    // Item 2: Milk
    // Item 3: Flour
    // Item 4: Baking Powder
    // Item 5: Bananas
    

    2 .集合Sets

    集合(Set)用来存储相同类型并且没有确定顺序的值。当集合元素顺序不重要时或者希望确保每个元素只出现一次时可以使用集合而不是数组。

    • 2.1 集合类型的哈希值

    一个类型为了存储在集合中,该类型必须是可哈希化的--也就是说,该类型必须提供一个方法来计算它的哈希值。一个哈希值是Int类型的,相等的对象哈希值必须相同,比如a==b,因此必须a.hashValue == b.hashValue
    Swift 的所有基本类型(比如String,Int,DoubleBool)默认都是可哈希化的,可以作为集合的值的类型或者字典的键的类型。没有关联值的枚举成员值默认也是可哈希化的。

    • 2.2 集合类型语法

    Swift 中的Set类型被写为Set<Element>,这里的Element表示Set中允许存储的类型,和数组不同的是,集合没有等价的简化形式。

    • 2.3 创建和构造一个空的集合

    你可以通过构造器语法创建一个特定类型的空集合:

    var letters = Set<Character>()
    print("letters is of type Set<Character> with \\(letters.count) items.")
    // Prints "letters is of type Set<Character> with 0 items."
    

    通过构造器,这里的letters变量的类型被推断为Set<Character>。
    .
    此外,如果上下文提供了类型信息,比如作为函数的参数或者已知类型的变量或常量,像数组一样,我们可以通过一个空的数组字面量创建一个空的Set

    letters.insert("a")
    // letters now contains 1 value of type Character
    letters = []
    // letters is now an empty set, but is still of type Set<Character>
    
    • 2.4 用数组数值创建集合

    你可以使用数组数值来构造集合,并且可以使用简化形式写一个或者多个值作为集合元素。
    下面的例子创建一个称之为favoriteGenres的集合来存储String类型的值:

    var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]
    // favoriteGenres has been initialized with three initial items
    

    一个Set类型不能从数组字面量中被单独推断出来,因此Set类型必须显式声明。然而,由于 Swift 的类型推断功能,如果你想使用一个数组字面量构造一个Set并且该数组字面量中的所有元素类型相同,那么你无须写出Set的具体类型。favoriteGenres的构造形式可以采用简化的方式代替:var favoriteGenres: Set = ["Rock", "Classical", "Hip hop"]
    由于数组字面量中的所有元素类型相同,Swift 可以推断出Set<String>作为favoriteGenres变量的正确类型。

    • 2.5 访问和修改一个集合

    你可以通过Set的属性和方法来访问和修改一个Set。基本和数组一样,只是Set是无序的没有索引。
    (1)和数组一样,为了找出一个Set中元素的数量,可以使用其只读属性count

    print("I have \\(favoriteGenres.count) favorite music genres.")
    // Prints "I have 3 favorite music genres."
    

    (2)和数组一样,使用布尔属性isEmpty作为一个缩写形式去检查count属性是否为0

    var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]
    if favoriteGenres.isEmpty {
    print("As far as music goes, I'm not picky.")
    } else {
    print("I have particular music preferences.")
    }
    // Prints "I have particular music preferences."
    

    (3)和数组一样,可以通过调用Set的insert(_:)方法来添加一个新元素:

    var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]
    favoriteGenres.insert("Jazz")
    // favoriteGenres now contains 4 items
    

    (4)和数组一样,Set通过调用remove(_:)方法去删除一个元素,如果该值是该Set的一个元素则删除该元素并且返回被删除的元素值,否则如果该Set不包含该值,则返回nil。另外,Set中的所有元素可以通过它的removeAll()方法删除。

    var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]
    favoriteGenres.insert("Jazz")
    if let removedGenre = favoriteGenres.remove("Rock") {
    print("\\(removedGenre)? I'm over it.")
    } else {
    print("I never much cared for that.")
    }
    // Prints "Rock? I'm over it."
    

    (5) 使用contains(_:)方法去检查Set中是否包含一个特定的值,数组也一样可以使用contains(_:)方法去检查。

    if favoriteGenres.contains("Funk") {
    print("I get up on the good foot.")
    } else {
    print("It's too funky in here.")
    }
    // Prints "It's too funky in here."
    

    (6)for-in遍历一个集合

    你同样可以用for-in循环遍历一个Set中的所有值

    for genre in favoriteGenres {
    print("\\(genre)")
    }
    // Jazz
    // Hip hop
    // Classical
    

    (7) sort()方法遍历

    Swift 的Set类型没有确定的顺序,为了按照特定顺序来遍历一个Set中的值可以使用sort()方法,它将返回一个有序数组,这个数组的元素排列顺序由操作符'<'对元素进行比较的结果来确定.

    for genre in favoriteGenres.sorted() {
    print("\\(genre)")
    }
    // Classical
    // Hip hop
    // Jazz
    
    • 2.6 集合操作

    你可以高效地完成Set的一些基本操作,比如把两个集合组合到一起,判断两个集合共有元素,或者判断两个集合是否全包含,部分包含或者不相交。就是大学你学的集合的概念
    (1) 基本集合操作
    下面的插图描述了两个集合 a 和 b 以及通过阴影部分的区域显示集合各种操作的结果。

    setVennDiagram_2x.png
    使用 intersect(_:) 方法根据两个集合中都包含的值创建的一个新的集合。(a ∩ b)
    使用 exclusiveOr(_:) 方法根据在一个集合中但不在两个集合中的值创建一个新的集合。
    使用 union(_:) 方法根据两个集合的值创建一个新的集合。(a ∪ b)
    使用 subtract(_:) 方法根据不在该集合中的值创建一个新的集合。
    let oddDigits: Set = [1, 3, 5, 7, 9]
    let evenDigits: Set = [0, 2, 4, 6, 8]
    let singleDigitPrimeNumbers: Set = [2, 3, 5, 7]
    oddDigits.union(evenDigits).sorted()
    // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    oddDigits.intersection(evenDigits).sorted()
    // []
    oddDigits.subtracting(singleDigitPrimeNumbers).sorted()
    // [1, 9]
    oddDigits.symmetricDifference(singleDigitPrimeNumbers).sorted()
    // [1, 2, 9]
    

    (2) 集合成员关系和相等

    下面的插图描述了三个集合a,bc,以及通过重叠区域表述集合间共享的元素。集合a是集合b的父集合,因为a包含了b中所有的元素,相反的,集合b是集合a的子集合,因为属于b的元素也被a包含。集合b和集合c彼此不关联,因为它们之间没有共同的元素。

    setEulerDiagram_2x.png
    使用“等于”运算符(==),以确定两套是否包含所有相同的价值观。
    使用isSubset(of:)方法,以确定是否所有的一组的值被包含在指定集合。
    使用isSuperset(of:)方法确定集是否包含在一组指定的所有值。
    使用isStrictSubset(of:)isStrictSuperset(of:)方法,以确定一组是否是一个子集或超集,但不等于,指定的一组。
    使用isDisjoint(with:)的方法来确定两个集合是否有任何共同的值。
    let houseAnimals: Set = ["", ""]
    let farmAnimals: Set = ["", "", "", "", ""]
    let cityAnimals: Set = ["", ""]
    houseAnimals.isSubset(of: farmAnimals)
    // true
    farmAnimals.isSuperset(of: houseAnimals)
    // true
    farmAnimals.isDisjoint(with: cityAnimals)
    // true
    

    3 .字典 Dictionary

    字典是一种存储多个相同类型的值的容器。每个值(value)都关联唯一的键(key),键作为字典中的这个值数据的标识符。和数组中的数据项不同,字典中的数据项并没有具体顺序。我们在需要通过标识符(键)访问数据的时候使用字典,这种方法很大程度上和我们在现实世界中使用字典查字义的方法一样。

    • 3.1 字典类型快捷语法

    Swift 的字典使用Dictionary<Key, Value>定义,其中Key是字典中键的数据类型,Value是字典中对应于这些键所存储值的数据类型。一个字典的Key类型必须遵循Hashable协议,就像Set的值类型,是不可以重复的

    • 3.2 创建一个空字典

    像数组一样使用构造语法创建一个拥有确定类型的空字典:

    var namesOfIntegers = [Int: String]()
    // namesOfIntegers is an empty [Int: String] dictionary
    

    这个例子创建了一个[Int: String]类型的空字典来储存整数的英语命名。它的键是Int型,值是String型。
    .
    如果上下文已经提供了类型信息,我们可以使用空字典数值来创建一个空字典,记作[:]

    namesOfIntegers[16] = "sixteen"
    // namesOfIntegers now contains 1 key-value pair
    namesOfIntegers = [:]
    // namesOfIntegers is once again an empty dictionary of type [Int: String]
    
    • 3.3 用字典数值创建字典

    我们可以使用字典数值来构造字典,这和我们刚才介绍过的数组数值拥有相似语法。字典数值是一种将一个或多个键值对写作Dictionary集合的快捷途径。
    一个键值对是一个key和一个value的结合体。在字典数值中,每一个键值对的键和值都由冒号分割。这些键值对构成一个列表,其中这些键值对由方括号包含、由逗号分割:

    [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"]
    
    • 3.4 访问和修改字典

    我们可以通过字典的方法和属性来访问和修改字典,或者通过使用下标语法
    (1) 和数组一样,我们可以通过字典的只读属性count来获取某个字典的数据项数量:

    print("The airports dictionary contains \\(airports.count) items.")
    // Prints "The airports dictionary contains 2 items."
    

    (2)使用布尔属性isEmpty来快捷地检查字典的count属性是否等0:

    if airports.isEmpty {
    print("The airports dictionary is empty.")
    } else {
    print("The airports dictionary is not empty.")
    }
    // Prints "The airports dictionary is not empty."
    

    (3) 我们也可以在字典中使用下标语法来添加新的数据项。可以使用一个恰当类型的键作为下标索引,并且分配恰当类型的新值,像OC一样

    airports["LHR"] = "London"
    // the airports dictionary now contains 3 items
    

    (4) 同样可以使用上面的方法修改值

    airports["LHR"] = "London Heathrow"
    // the value for "LHR" has been changed to "London Heathrow"
    

    (5) 字典的updateValue(_:forKey:)更新值

    字典的updateValue(_:forKey:)方法可以设置或者更新特定键对应的值。就像上面所示的下标示例,updateValue(_:forKey:)方法在这个键不存在对应值的时候会设置新值或者在存在时更新已存在的值。和上面的下标方法不同的,updateValue(_:forKey:)这个方法返回更新值之前的原值。这样使得我们可以检查更新是否成功。

     var airports = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
      if let oldValue = airports.updateValue("Dublin Airport", forKey: "DUB")
      {
         print("The old value for DUB was \\(oldValue).")   
       }
    else
     {
         print("The old value was nil")
      } 
     // Prints "The old value for DUB was Dublin."
    

    当更新的键不存在时,返回nil

     var airports = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]
      if let oldValue = airports.updateValue("Dublin Airport", forKey: "DU")
      {
         print("The old value for DU was \\(oldValue).")   
       }
    else
     {
         print("The old value was nil")
      } 
     // Prints "The old value was nil"
    

    (6) 我们也可以使用下标语法来在字典中检索特定键对应的值。因为有可能请求的键没有对应的值存在,字典的下标访问会返回对应值的类型的可选值。如果这个字典包含请求键所对应的值,下标会返回一个包含这个存在值的可选值,否则将返回nil:

    if let airportName = airports["DUB"] {
    print("The name of the airport is \\(airportName).")
    } else {
    print("That airport is not in the airports dictionary.")
    }
    // Prints "The name of the airport is Dublin Airport."
    

    (6)nil 移除一个键值对

    我们还可以使用下标语法来通过给某个键的对应值赋值为nil来从字典里移除一个键值对:

    airports["APL"] = "Apple International"
    // "Apple International" is not the real airport for APL, so delete it
    airports["APL"] = nil
    // APL has now been removed from the dictionary
    

    (7) removeValueForKey(_:)方法移除键值对

    这个方法在键值对存在的情况下会移除该键值对并且返回被移除的值或者在没有值的情况下返回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."
    
    • 3.5 字典遍历
      (1) 我们可以使用for-in循环来遍历某个字典中的键值对。每一个字典中的数据项都以(key, value)元组形式返回,并且我们可以使用临时常量或者变量来分解这些元组:
    for (airportCode, airportName) in airports {
    print("\\(airportCode): \\(airportName)")
    }
    // YYZ: Toronto Pearson
    // LHR: London Heathrow
    

    (2) 通过访问keys或者values属性来遍历字典的键或者值:

    for airportCode in airports.keys {
    print("Airport code: \\(airportCode)")
    }
    // Airport code: YYZ
    // Airport code: LHR
    for airportName in airports.values {
    print("Airport name: \\(airportName)")
    }
    // Airport name: Toronto Pearson
    // Airport name: London Heathrow
    

    (3) 用字典的keysvalues属性实例初始化与一个新的数组

    let airportCodes = [String](airports.keys)
    // airportCodes is ["YYZ", "LHR"]
    let airportNames = [String](airports.values)
    // airportNames is ["Toronto Pearson", "London Heathrow"]
    

    以上只是一些简单的概念说(fan)明(yi),如有错误请指正,谢谢。Swift 3 学习 ,后续会不断更新。
    如果你觉得我的文章对你有帮助请点喜欢哦,也可以关注我,每周至少一篇技术更新。
    或者关注 我的专题 每周至少5篇高质量文章收录,多谢支持。

    一起学习,一起进步

    相关文章

      网友评论

        本文标题:Swift 3 学习 - 04 集合类型

        本文链接:https://www.haomeiwen.com/subject/nymuyttx.html