《浅入浅出Swift》Array,Dictionary 数组和字典

####Array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

// var myArrayOld : String[] = ["var1","var2","var3"]
//Xcode6 beta3
var myArrayOld :[String] = ["var1","var2","var3"]
//简写为
var myArray = ["var1","var2","var3"];
//混杂类型
var myArrayMix = ["string1",1,2]
//空array
// var myInts = Int[] ()
//Xcode6 beta3
var myInts = [Int] ()
// repeatedValue重复值
// var myDouble = Double[](count:3,repeatedValue:0.0)
//20140707 Xcode6 beta3
var myDouble = [Double](count:3,repeatedValue:0.0)

//数量
println("数组数量 \(myArray.count)")

//返回用于判断array是不是空
println("数组是否为空 \(myArray.isEmpty)")//false

//在最后追加元素
myArray.append("hello")
println("数组最后添加元素 \(myArray)")

//+= 也可以添加元素
myArray += "world"
println("数组最后添加元素 \(myArray)")

//指定位置插入
myArray.insert("need",atIndex:0)
println("数组插入数据 \(myArray)")

//删除指定下标位置元素
myArray.removeAtIndex(0)
println("数组删除指定下标元素 \(myArray)")

//删除最后一个元素
myArray.removeLast()
println("数组删除最后一个元素 \(myArray)")

//删除所有元素
// myArray.removeAll(keepCapacity:false)
//另一种方法
// myArray = []
// println("数组删除所有元素 \(myArray)")

//范围下标
myArray[2...3] = ["china"]
println("数组范围后 [x...x]<[] \(myArray)")

//如果 [x...x]> [] 那么数组不会出错,连续插入
//越界依然会崩溃 [2...3]
myArray[1...2] = ["china","shanghai","us"]
println("数组范围后 [x...x]>[] \(myArray)")

//下标语法subscript syntax
var myFirst = myArray[0]
println("数组第一个元素是 \(myFirst)")

//遍历
for item in myArray{
println(item)
}

//enumerate
for (index, value)in enumerate(myArray){
println("item \(index+1):\(value)")
}

####Dictionary

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

//所有Swift的基本数据类型,String, Int, Double, Bool 全都可以做为key
// var myDict : Dictionary<String,String> = ["china":"panda","australia":"kangaroo"]
var myDict = ["china":"panda","australia":"kangaroo"]

//创建空Dict
var myKong = Dictionary <String,String>()
//清空Dict
myKong = [:]

//Dict中key-value的数量
println("字典数量 \(myDict.count)")

//改某个key的值
myDict["australia"] = "duckbill"

//遍历
for (country, animal)in myDict{
println("counter:\(country),animal:\(animal)")
}

//单独遍历key
for country in myDict.keys{
println("myDict的key \(country)")
}

//单独遍历value
for animal in myDict.values{
println("myDict的values \(animal)")
}

//key拿出创建array
var myKeyArr = Array(myDict.keys)
//value拿出创建array
var myValueArr = Array(myDict.values)

Reference

坚持原创技术分享,您的支持将鼓励我继续创作!
0%