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

Swift学习笔记- 4.集合类型

时间:2015-02-15 20:37:22      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

数组

 
数组的简单语法
写数组应遵循 Array<SomeType> 这样的形式,也可以使用 [someType] 这样的简单语法。推荐使用更短的
 
数组构造语句
数组字面量: [value1, value2, value3]
 
var shoppingList: [String] = ["Eggs", "Milk]
 
由于类型推断机制,当我们用字面量构造只拥有相同类型数值的时候,我们不必把数组的类型定义写清楚。还可以这么写:
 
var shoppingList = ["Eggs", "Milk"]
 
访问和修改数组
(1)我们可以通过数组的方法和属性来访问和修改数组,或者下标语法。还可以用数组的制度属性 count 来获取数组中的数据项数量。
 
println("The shopping list contains \(shoppingList.count) items")
 
(2)使用 isEmpty 来检查数据项是否为0
 
if shoppingList.isEmpty {
    println("The shopping list is empty")
} else {
    println("The shopping list is not empty")
}
 
(3)可以使用 append 方法在数组后面添加新的数据项
 
shoppingList.append("Flour")
 
(4)使用 += 也可以在数组后添加一个或多个拥有相同类型的数据项:
 
shoppingList += ["Baking Powder"]
shoppingList += ["Chocolate Spread", "Cheese", "Butter"]
 
(5)可以使用下标语法来获取数据项,或修改某个索引值对应的数据值
 
var firstItem = shoppingList[0]
shoppingList[0] = "Six eggs"
 
(6)可以利用下标来一次改变一系列数据值,即使新数据和原有数据的数量不一样。
 
shoppingList[4...6] = ["Banana", "Apples"]
 
(7)调用 inser(atIndex:) 方法在某个具体索引值之前添加数据项
 
shoppingList.insert("Maple Syrup", atIndex: 0)
 
(8)同样也可以使用 removeAtIndex 方法来一出数组中的某一项。移除某一项,并返回被移除的数据项
 
let mapleSyrup = shoppingList.removeAtIndex(0)
let apples = shoppingList.removeLast()
 
数组的遍历
 
(1)通过 for in 循环遍历

for item in shoppingList {
    println(item)
}
 
(2)如果同时需要每个数据项的值和索引值,可以使用全局 enumerate 函数来进行数组遍历。enumerate 返回一个由每一个数据项索引值和数据值组成的元组。我们可以把这个元组分解成临时常量或者变量来进行遍历

for (index, value) in enumerate(shoppingList) {
    println("Item \(index + 1) : \(value)")
}
 
创建并构造一个数组
 
使用构造语法来创建一个空数组
 
var someInts = [Int]()
println("someInts is of type Int[] with \(someInts.count) items")
 
如果代码上下文中提供了类型信息,例如一个函数参数或一个已经定义好类型的常量或者变量,可以使用空数组语句创建一个空数组,写法很简单: [ ]
 
someInts.append(3)
someInts = []
 
Array 类型还提供一个可以创建特定大小并所有数据都被默认的构造方法。可以把准备加入新数组的数据项的数量(count)和适当类型的初始值(repeatedValue)传入数组构造函数:
 
var threeDoubles = [Double](count: 3, repeatedValue: 0.0)
 
因为类型推断,也可以直接这样写:
 
var anotherThreeDouble = Array(count: 3, repeatedValue: 2.5)
 
最后,可以使用 “ + ” 来组合两种已存在的相同类型数组。新数组的类型会从两个数组的数据类型中推断出来:
 
var sixDoubles = threeDoubles + anotherThreeDouble
 
 
 
字典
 
Swift 的字典使用时需要具体规定可以存储键和值的类型。
 
Swift 的字典使用 Dictionary<KeyType, ValueType> 定义,你也可以使用短语法 [KeyType: valueType].
 
定义字典:
[key 1: value 1, key 2: value 2, key 3: value 3]
 
var airports: [String: String] = ["TYO": "Tokyo", "DUB": "Dubliln"]
 
和数组一样,如果使用字面量构造字典就不用把类型定义清楚。airports 也可以这样定义:
var airports2 = ["TYO": "Tokyo", "DUB": "Dubliln]
 
读取和修改字典
 
(1)和数组一样,可以通过字典的只读属性 count 来获取某个字典的数据项数量:
 
println("The airports dictionary contains \(airports.count) items)
 
(2)使用 isEmpty 来判断 count 是否为0:
 
if airports.isEmpty {
    println("The airports dictionary is empty.")
} else {
    println("The airports dictionary is not empty.")
}
 
(3)我们也可以通过下标语法来添加新数据项,或者改变对应的数据项
 
airports["LHR"] = "London"
airports["LHR"] = "London Heathrow"
 
(4)updateValue(forKey:) 方法可以设置或者更新特定键对应的值。不过与下标方法不同的时,这个函数会返回更新值之前的原值。
updateValue(forKey:) 会返回包含一个字典值类型的可选值。
 
if let oldValue = airports.updateValue("Dublin International", forKey: "DUB") {
    println("The old value for DUB was \(oldValue)")
}
 
(5)可以使用下标语法在字典中检索特定值对应的值。如果该键对应的值存在,则返回值。否则返回 nil
 
if let airportName = airports["DUB"] {
    println("The name of the airport is \(airportName)")
} else {
    println("That airport is not in the airports dictionary")
}
 
(6)还可以使用下标语法来通过给某个键对应的值赋值为 nil 来从字典里移除一个键值对:
 
airports["APL"] = nil
 
(7)removeValueForKey 方法也可以用来在字典中移除键值对。这个方法在键值对存在的情况下会移除该键值对,并且返回被移除的 value 或在没有值的情况下返回 nil
 
if let removedValue = airports.removeValueForKey("DUB") {
    println("The removed airport‘s name is \(removedValue)")
} else {
    println("The airports dictionary does not contain a value for DUB")
}
 
遍历字典
 
我们可以使用for in 来遍历某个字典中的键值对。每一个字典中的数据项都有(key,value)元组形式返回,并且可以使用临时常量或变量来分解这些元组。
 
for(airportCode, airportName) in airports {
    println("\(airportCode):\(airportName)")
}
 
我们也可以通过访问它的 keys 或者 values 属性(都是可遍历集合)检索一个字典的键或值
 
for airportCode in airports.keys {
    println("Airport code: \(airportCode)")
}

for airportName in airports.values {
    println("Airport name: \(airportName)")
}
 
如果只是需要使用某个字典的键值对或者值集合来作为某个接受 Array 实例 API 的参数,可以使用 keys 或 values 属性直接构造一个新数组 
 
let airportCodes = [String](airports.keys)
let airportNames = [String](airports.values)
 
 
创建一个空字典
我们也可以像数组一样使用构造语法创建一个空字典:
 
var namesOfIntegers = [Int: String]()
 
如果上下文已经提供了信息类型,我们可以使用空字典字面量来创建一个空字典,记作:[ : ]
 
namesOfIntegers[16] = "Sixteen"
namesOfIntegers = [:]
 

Swift学习笔记- 4.集合类型

标签:

原文地址:http://www.cnblogs.com/kangshang/p/4293438.html

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