swift2 集合类型

前端之家收集整理的这篇文章主要介绍了swift2 集合类型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

数组


创建一个空数组


  1. var someInt = [Int]()
  2. print("someInt is of type [int] with \(someInt.count) items")


创建有默认值的数组


  1. var someInt = [Double](count: 3,repeatedValue: 0.0)
// [0.0,0.0,0.0]


数组相加


  1. var someInt = [Double](count: 3,repeatedValue: 0.0)
  2. var anotherInt = [Double](count: 3,repeatedValue: 2.5)
  3. someInt += anotherInt
// [0.0,2.5,2.5]


修改数组值


  1. someInt[3...5] = [1.0,2.0]
  2. for num in someInt{
  3. print(num)
  4. }

// 将3个2.5改为1.0 2.0,此时共有5个值


遍历数组


除了上面的方式,还可以在遍历的时候拿到值的索引

  1. for (index,num) in someInt.enumerate(){
  2. print("item \(index+1):\(num)")
  3. }



集合


创建一个集合


  1. var myset = Set<Character>()
  2. print(myset.count)
  3. myset.insert("c")
  4. print(myset.count)


数组字面值初始化集合


  1. var favoriteGenres: Set<String> = ["Rock","Classical","Hip hop"]
  2. // favoriteGenres被构造成含有三个初始值的集合


遍历集合


  1. var favoriteGenres: Set<String> = ["Rock","Hip hop"]
  2. // favoriteGenres被构造成含有三个初始值的集合
  3. for genre in favoriteGenres {
  4. print("\(genre)")
  5. }


集合的关系操作







集合比较


使用“是否等”运算符(==)来判断两个集合是否包含相同的值。
使用isSubsetOf(_:)方法来判断一个集合中的值是否也被包含在另外一个集合中。
使用isSupersetOf(_:)方法来判断一个集合中包含的值是另一个集合中所有的值。
使用isStrictSubsetOf(_:)或者isStrictSupersetOf(_:)方法来判断一个集合是否是另外一个集合的子集合或者父集合并且和特定集合不相等。
使用isDisjointWith(_:)方法来判断两个结合是否不含有相同的值。


字典


创建字典


  1. var mydic = [Int:String]()
  2. mydic[1]="one"


数组字面值初始化字典


  1. var airports: [String:String] = ["TYO": "Tokyo","DUB": "Dublin"]


读取和修改字典


  1. var airports: [String:String] = ["TYO": "Tokyo","DUB": "Dublin"]
  2.  
  3. print("The dictionary of airports contains \(airports.count) items.")
  4. // 打印 "The dictionary of airports contains 2 items."(这个字典有两个数据项)


  1. if airports.isEmpty {
  2. print("The airports dictionary is empty.")
  3. } else {
  4. print("The airports dictionary is not empty.")
  5. }
  6. // 打印 "The airports dictionary is not empty.(这个字典不为空)"


  1. airports["LHR"] = "London"
  2. // airports 字典现在有三个数据项

  1. airports["LHR"] = "London Heathrow"
  2. // "LHR"对应的值 被改为 "London Heathrow

  1. if let oldValue = airports.updateValue("Dublin Internation",forKey: "DUB") {
  2. print("The old value for DUB was \(oldValue).")
  3. }
  4. // 输出 "The old value for DUB was Dublin."(DUB原值是dublin)

updateValue(forKey:)函数会返回包含一个字典值类型的可选值。
举例来说:对于存储String值的字典,这个函数会返回一个String?或者“可选 String”类型的值。
如果值存在,则这个可选值值等于被替换的值,否则将会是nil。


  1. airports["APL"] = nil
  2. // APL现在被移除了

另外,removeValueForKey方法也可以用来在字典中移除键值对。
这个方法在键值对存在的情况下会移除该键值对并且返回被移除的value或者在没有值的情况下返回nil:

  1. if let removedValue = airports.removeValueForKey("DUB") {
  2. print("The removed airport's name is \(removedValue).")
  3. } else {
  4. print("The airports dictionary does not contain a value for DUB.")
  5. }


字典遍历


  1. for (airportCode,airportName) in airports {
  2. print("\(airportCode): \(airportName)")
  3. }
  4. // TYO: Tokyo
  5. // LHR: London Heathrow


也可以通过访问它的keys或者values属性(都是可遍历集合)检索一个字典的键或者值:

  1. for airportCode in airports.keys {
  2. print("Airport code: \(airportCode)")
  3. }
  4. // Airport code: TYO
  5. // Airport code: LHR
  6.  
  7. for airportName in airports.values {
  8. print("Airport name: \(airportName)")
  9. }
  10. // Airport name: Tokyo
  11. // Airport name: London Heathrow


如果我们只是需要使用某个字典的键集合或者值集合来作为某个接受Array实例 API 的参数,
可以直接使用keys或者values属性直接构造一个新数组:

  1. let airportCodes = Array(airports.keys)
  2. // airportCodes is ["TYO","LHR"]
  3.  
  4. let airportNames = Array(airports.values)
  5. // airportNames is ["Tokyo","London Heathrow"]

猜你在找的Swift相关文章