Swift 2.1 Examples - Collection Types

The Swift Programming Language 2.1 Examples

源码在 GitHub:https://github.com/gewill/The-Swift-Programming-Language-2.1-Examples

Playground ->

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//: Playground - noun: a place where people can play

import UIKit

//数组
var array1 = [Double](count: 3, repeatedValue: 4.3)
print(array1)

var array2 = array1 + array1

if array1.isEmpty {
print("array1 is empty.")
} else {
print("array1 is not empty, have \(array1.count) numbers.")}

array1.insert(3.2, atIndex: 0)

print(array1)

for item in array1 {
print(item)
}

//集合
var letters = Set<Character>()
letters.insert("a")
letters = []
var set1: Set<String> = ["aaa", "b"]
var set2: Set = ["aaa", "b"]
set2.count
set2.insert("33")
set2.remove("b")
set2
for genre in set2.sort() {
print(genre)
}

set1.union(set2)
set1.subtract(set2)
set1.exclusiveOr(set2)
set1.intersect(set2)

let set3: Set<String> = ["33"]

set3.isDisjointWith(set2)
set3.isStrictSubsetOf(set2)
set3.isSubsetOf(set2)


//字典
var dict = [Int: String]()
dict[12] = "12222"
dict = [:]
dict = [12: "1222", 23: "232423", 322: "23dsfsaf"]
for (num, value) in dict {
print("\(num) is \(value)")
}
dict[12] = "tw"

for value in dict.values {
print(value)
}

//控制流
var i = 3
while i < 0 {
print(1)
}

//元组
let somePoint = (0, 1)

switch somePoint {
case (0, 0):
print("0")
case (_, 0):
print("is on the x-axis")
case (0, _):
print("is on the y-axis")
default:
print("is outside of the box")
}


//检测API是否可用
if #available(iOS 9, *) {
print(2)
} else {
print(1)
}

//if
var temperatureInFahrenheit = 90
if temperatureInFahrenheit <= 32 {
print("It's very cold. Consider wearing a scarf.")
} else if temperatureInFahrenheit >= 86 {
print("It's really warm. Don't forget to wear sunscreen.")
} else {
print("It's not that cold. Wear a t-shirt.")
}
//


//switch 默认遇到符合case,立即终止
let someCharacter: Character = "e"
switch someCharacter {
case "a", "e", "i", "o", "u":
print("\(someCharacter) is a vowel")
case "b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
"n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z":
print("\(someCharacter) is a consonant")
default:
print("\(someCharacter) is not a vowel or a consonant")
}
//

//where
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let (x, y) where x == y:
print("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y:
print("(\(x), \(y)) is on the line x == -y")
case let (x, y):
print("(\(x), \(y)) is just some arbitrary point")
}
//

//Continue
let puzzleInput = "great minds think alike"
var puzzleOutput = ""
for character in puzzleInput.characters {
switch character {
case "a", "e", "i", "o", "u", " ":
continue
default:
puzzleOutput.append(character)
}
}
print(puzzleOutput)
// prints "grtmndsthnklk”

//break
let numberSymbol: Character = "二" // Simplified Chinese for the number 3
var possibleIntegerValue: Int?
switch numberSymbol {
case "1", "١", "一", "๑":
possibleIntegerValue = 1
case "2", "٢", "二", "๒":
possibleIntegerValue = 2
print("test")
break
case "3", "٣", "三", "๓":
possibleIntegerValue = 3
case "4", "٤", "四", "๔":
possibleIntegerValue = 4
default:
break
}
if let integerValue = possibleIntegerValue {
print("The integer value of \(numberSymbol) is \(integerValue).")
} else {
print("An integer value could not be found for \(numberSymbol).")
}
// prints "The integer value of 三 is 3."

//Fallthrough
let integerToDescribe = 5
var description = "The number \(integerToDescribe) is"
switch integerToDescribe {
case 2, 3, 5, 7, 11, 13, 17, 19:
description += " a prime number, and also"

default:
description += " an integer."
}
print(description)
//

//提前退出
func greet(person: [String: String]) {
guard let name = person["name"] else {
return
}
print("Hello \(name)")

guard let location = person["location"] else {
print("I hope the weather is nice near you.")
return
}
print("I hope the weather is nice in \(location).")
}
greet(["name": "John"])
// prints "Hello John!"
// prints "I hope the weather is nice near you."
greet(["name": "Jane", "location": "Cupertino"])
// prints "Hello Jane!"
// prints "I hope the weather is nice in Cupertino."