zoukankan      html  css  js  c++  java
  • Swift--集合类型 数组 字典 集合

    数组

    1.创建一个数组

      var someInts = [Int]()空数组

      someInts = []清空

      var threeDoubles = Array(repeating: 0.0, count: 3)有默认值的数组

      var shoppingList: [String] = ["Eggs", "Milk"]

      var arra = arrb + arrc创建一个数组是另两个数组的相加

    2.array.count

    3.array.isEmpty

    4.加元素

      array.append("Flour")

      array += ["Baking Powder"]

    5.插入

      array.insert("Maple Syrup", at: 0)

    6.删除

      let a = array.remove(at: 0)

    7.遍历

      for item in array {

          print(item)

      }

      for (index, value) in array.enumerated() {

          print("Item (index + 1): (value)")

      }

    集合

    1.创建一个新的集合

      var letters = Set<Character>()

      letters = []清空

      var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]

    2.favoriteGenres.count

    3.favoriteGenres.isEmpty

    4.操作

      favoriteGenres.insert("Jazz")

      favoriteGenres.remove("Rock")

      favoriteGenres.contains("Funk")是否存在

    5.遍历

      for genre in favoriteGenres {

          print("(genre)")

      }

      for genre in favoriteGenres.sorted() {//按从小到大遍历

          print("(genre)")

      }

    6.集合关系

      

     

      intersect()两个集合中都包含的值创建的一个新的集合。

      exclusiveOr()只在一个集合中但不在两个集合中的值创建一个新的集合。

      union()两个集合的值创建一个新的集合。

      subtract()不在该集合中的值创建一个新的集合。

    7.集合关系

      

     

      isSubset(Of)判断一个集合中的值是否也被包含在另外一个集合中。

      isSuperset(Of)判断一个集合中包含的值是否含有另一个集合中所有的值。

      isStrictSubset(Of)或者isStrictSuperset(Of)判断一个集合是否是另外一个集合的子集合或者父集合并且和特定集合不相等。

      isDisjoint(With)判断两个集合是否不含有相同的值。

    字典

    1.创建一个新的字典

      var namesOfIntegers = [Int: String]()

      namesOfIntegers[16] = "sixteen"如果没有key就加,如果有key就覆盖原来的值

      namesOfIntegers = [:]清空

      var airports: [String: String] = ["YYZ": "Toronto Pearson", "DUB": "Dublin"]

    2.airports.count

    3.airports.isEmpty

    4.操作

      let oldValue = airports.updateValue("Dublin Airport", forKey: "DUB")更新

      airports["APL"] = nil删除

      let removedValue = airports.removeValue(forKey: "DUB")删除

    5.遍历

      for (airportCode, airportName) in airports {

      }

      for airportCode in airports.keys {

      }

      for airportName in airports.values {

      }

      let airportCodes = [String](airports.keys)

      let airportNames = [String](airports.values)

  • 相关阅读:
    Swift之 ? 和 !
    objective-c工程使用swift
    NSMethodSignature和NSInvocation的用法
    NSTimer
    iOS 的 XMPPFramework
    计算string高度
    python学习:猜数字小游戏
    python 各种控制语句
    ③ 小程序的代码组成
    ③ 组件&props
  • 原文地址:https://www.cnblogs.com/huoran1120/p/6114097.html
Copyright © 2011-2022 走看看