您的位置:首页 > 移动开发 > Swift

寒城攻略:Listo 教你 25 天学会 Swift 语言 - 06 Collection Types

2014-07-16 09:26 507 查看
import Foundation

//***********************************************************************************************

//1.Collection Types(集合类型)

//_______________________________________________________________________________________________

//介绍

//Swift 提供了两个集合类型,称为数组和字典,用于存储值的集合。数组存储相同类型的值的有序列表。字典存储无序相同类型的值的集合,它可以通过一个惟一的标识符来引用和抬头(有时也称为一个键)。

//数组和字典在 Swift
总是用值和键的类型存储。这意味着我们不能将错误类型的值插入数组或字典。这也意味着你可以有信心的类型值将从数组或词典检索。Swift
使用显式类型的集合可以确保代码总是清楚的值类型

//***********************************************************************************************

//2.Mutability of Collections(可变的集合类型)

//_______________________________________________________________________________________________

//定义与特性

//如果您创建一个数组或一个字典,并将它分配给一个变量,创建的集合将可变的。这意味着我们可以改变(突变)收集后由添加、删除或修改集合中的项目。相反,如果你分配一个数组或一个字典一个常量,数组或字典是不变的,它的大小和内容不能更改

//***********************************************************************************************

//3.Arrays(数组)

//_______________________________________________________________________________________________

//定义与特性

//相同类型的数组存储多个值的有序列表。数组中相同的值可以出现多次在不同的位置

//Swift 数组可以存储特定类型的值。他们不同于 Objective-C
的 NSArray 和 NSMutableArray
类,它可以存储任何类型的对象,不提供任何信息返回的对象的本质。在 Swift
中,特定的值类型数组可以存储总是明确表示,通过显式类型注释,或通过类型推断。

//_______________________________________________________________________________________________

//Swift 定义一个数组标准格式
var shoppingList: [String] = ["Eggs",
"Water", "Paper"]

println(shoppingList)

//_______________________________________________________________________________________________

//通过 Swift
中的类型推导简化定义数组
var shoppingList1 = ["Eggs",
"Paper", "Water"]

println(shoppingList1)

//_______________________________________________________________________________________________

//访问数组的个数

println("The shopping list contains
\(shoppingList.count) item")
//通过 ".count"
方法访问数组中元素的个数

//_______________________________________________________________________________________________

//访问数组是否为空
if
shoppingList.isEmpty{
//通过 ".isEmpty"
方法访问数组是否为空

println("The shopping list is empty")
}

else{

println("The shopping list is not empty")
}

//_______________________________________________________________________________________________

//为数组添加元素

shoppingList.append("Flour")
//通过 ".append(***)"
方法给数组添加元素

println(shoppingList)

shoppingList +=
"Baking Powder"
//通过 +=
算数运算符为数组添加元素

println(shoppingList)

shoppingList += ["Toy",
"bread"]
//通过 +=
算数运算符为数组添加数组

println(shoppingList)

shoppingList.insert("Maple Syrup", atIndex:
0)
//通过 ".insert(**, atIndex:**)"
函数来为数组固定位置添加元素

println(shoppingList)

//_______________________________________________________________________________________________

//使用下标检索数组元素

println("the first item of shoppingList is
\(shoppingList[0])")

//_______________________________________________________________________________________________

//修改数组元素
shoppingList[2] =
"meat"
//通过索引下标修改数组元素的值

println(shoppingList)

shoppingList[3...5] = ["Bananas",
"Apples"]
//通过索引下标范围修改范围内元素的值

println(shoppingList)

//_______________________________________________________________________________________________

//在数组中移除元素

let mapleSyrup =
shoppingList.removeAtIndex(0)
//使用 ".removeAtIndex()"
来移除数组中固定的某个元素

println(mapleSyrup)

println(shoppingList)

let mapleSyrup1 =
shoppingList.removeLast()
//使用 ".removeLast()"
来移除数组最后一个元素

println(mapleSyrup1)

println(shoppingList)

//shoppingList.removeAll() //使用 ".removeAll()"
来移除数组中的所有元素

println(shoppingList)

//_______________________________________________________________________________________________

//获取数组中的每一个元素

for item in
shoppingList{

println(item)
}

//_______________________________________________________________________________________________

//获取数组中单个的序号和元素
for (index, value)
in enumerate(shoppingList){

println("Item
\(index + 1):\(value)")
}

//_______________________________________________________________________________________________

//创建和初始化数组
var someInts = [Int]()
//创建一个空的 Int
类型数组

println("someInts is of type [Int] with
\(someInts.count) items")

someInts.append(3)
//为数组添加元素

println(someInts)

someInts = []
//数组为空

println(someInts)

//_______________________________________________________________________________________________

//批量初始化数组
var threeDoubles = [Double](count:
3, repeatedValue:
0.0)
var anotherThreeDoubles = [Double](count:
3, repeatedValue:
1.1)

var sixDoubles =
threeDoubles + anotherThreeDoubles

println(sixDoubles)

//***********************************************************************************************

//4.Dictionaries(字典)

//_______________________________________________________________________________________________

//定义与特性

//字典是一种存储相同类型多重数据的存储器。每个值(value)
都关联独特的键(key),键作为字典中这个值数据的标识符。和数组中的数据选项不同,字典中的数据项没有具体的顺序。我们需要通过标识符(key)
访问数据时使用字典

//_______________________________________________________________________________________________

//字典的标准格式
var airports:[String:
String] = ["TYO":
"Tokyo", "DUB":
"Dublin"]

println(airports)

//_______________________________________________________________________________________________

//字典的操作

println("The dictionary of airports contains
\(airports.count) items")
//使用 ".count"
函数获取字典中的元素对的值

//_______________________________________________________________________________________________

//字典的添加与修改

airports["LHR"] =
"Lodon"
//通过下标索引字典中的 key
值,如果字典中存在 key 值,那么这个 key
对应的 value
被表达式中的 value 覆盖,如果不存在 key
值,就为字典添加一对 key
和 value

println(airports)

//_______________________________________________________________________________________________

//使用 updateValue(forKey:)
方法更新 key 对应的 value
值,并且返回一个可选值的字典的值类型,然后这个方法返回的值为 key
原来对应的 value
if
let oldValue = airports.updateValue("Dublin International", forKey:
"DUB"){

println("The old value for DUB was
\(oldValue)")
}

if
let airportName =
airports["DUB"]{
//输出更新后的 key
对应的 value


println("The name of the airport is
\(airportName)")
}

else{

println("The airport is not in dictionary")
}

//_______________________________________________________________________________________________

//字典元素的删除

airports["LHR"] =
nil
//通过下标访问 key
值然后设置 value 为 nil
即可删除字典元素

println(airports)

if let removedValue =
airports.removeValueForKey("DUB"){
//使用 ".removeValueForKey(**)"
方法移除元素的,返回值为该 key
对应的 value

println("The removed airport's name is
\(removedValue)")
}

else{

println("the airports dictionary does not contain the value for DUB")
}

println(airports)

//_______________________________________________________________________________________________

//遍历字典
for (airportCode, airportName)
in airports{
//遍历字典

println("\(airportCode):\(airportName)")
}

//_______________________________________________________________________________________________

//遍历字典中所有的 key
for airportCode
in airports.keys{

println("airprots code is :\(airportCode)")
}

//_______________________________________________________________________________________________

//遍历字典中所有的 value
for airportName
in airports.values{

println("airports name is :\(airportName)")
}

//_______________________________________________________________________________________________

//获取字典中所有的 key
和 value
let airportCodes = [String](airports.keys)
//使用 ".keys"
获取字典中所有的 key

let airportNames = [String](airports.values)
//使用 ".value"
获取字典中所有的 value

println("the keys of dictionary is:
\(airportCodes)")

println("the values of dictionary is:
\(airportNames)")

//_______________________________________________________________________________________________

//创建一个空的字典
var namesOfIntegers = [Int:
String]()

namesOfIntegers[1] =
"one"
//为字典添加元素

println(namesOfIntegers)

namesOfIntegers = [:] //字典为空
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: