【iOS】First Taste of Swift with Playgrounds

mark,记录之前写的基础swift代码。后续记得整理swift开发的demo APP。

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
199
200
201
import UIKit

var str = "Hello, playground"

let constant = 10
var number = 10
var result = number + constant

//constant = 20
// Cannot assign to value: 'constant' is a 'let' constant

number = 50

var message = "The best way to get started is to stop talking and code."

var greeting = "Hello "
var name = "Yofer"
message = greeting + name

message.uppercased()
message.lowercased()

message.count

var bookPrice = 39
var numOfCopies = 5
var totalPrice = bookPrice * numOfCopies
//var totalPriceMessage = "The price of the book is $" + totalPrice
// Binary operator '+' cannot be applied to operands of type 'String' and 'Int'

var totalPriceMessage = "The price of the book is $" + String(totalPrice)

totalPriceMessage = "The price of the book is $\(totalPrice)"



var timeYouWakeUp = 6

if timeYouWakeUp == 6 {
print("Cook yourself a big breakfast")
} else {
print("Go out for breakfast")
}


switch timeYouWakeUp {
case 6:
print("Cook yourself a big breakfast")
default:
print("Go out for breakfast")
}


var bonus = 5000

if bonus >= 10000 {
print("I will travel to Paris and London!")
} else if bonus >= 5000 && bonus < 10000 {
print("I will travel to Tokyo")
} else if bonus >= 1000 && bonus < 5000 {
print("I will travel to Bangkok")
} else {
print("Just stay home")
}


do {
var bonus = 5000
// Variable 'bonus' was never mutated; consider changing to 'let' constant

switch bonus {
case 10000...:
print("I will travel to Paris and London!")
case 5000...9999:
print("I will travel to Tokyo")
case 1000...4999:
print("I will travel to Bangkok")
default:
print("Just stay home")
}
}


//var bookCollection = ["Tool of Titans", "Rework", 1]
// error: heterogeneous collection literal could only be inferred to '[Any]'; add explicit type annotation if this is intentional

var bookCollection = ["Tool of Titans", "Rework", "Your Move"]

bookCollection[0]

bookCollection.append("Authority")
bookCollection.count


print(bookCollection[0])
print(bookCollection[1])
print(bookCollection[2])
print(bookCollection[3])

for index in 0...3 {
print(bookCollection[index])
}


for index in 0...bookCollection.count - 1 {
print(bookCollection[index])
}

for book in bookCollection {
print(book)
}


var bookCollectionDict = ["1328683788" : "Tool of Titans", "0307463745" : "Rework", "1612060919" : "Authority"]

bookCollectionDict["1328683788"]

bookCollectionDict["321"]

for (key, value) in bookCollectionDict {
print("ISBN: \(key)")
print("Title: \(value)")
}


var emojiDict = ["馃懟": "Ghost",
"馃挬": "Poop",
"馃槫": "Angry",
"馃槺": "Scream",
"馃懢": "Alien monster"]

var wordToLookup = "馃懟"
var meaning = emojiDict[wordToLookup]

print(meaning)
// Expression implicitly coerced from 'String?' to 'Any'

wordToLookup = "馃槫"
meaning = emojiDict[wordToLookup]

print(meaning)
// Expression implicitly coerced from 'String?' to 'Any'

meaning = emojiDict["馃槏"]
if let meaning = meaning {
print(meaning)
}

let containerView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
containerView.backgroundColor = UIColor.orange

let emojiLabel = UILabel(frame: CGRect(x: 95, y: 20, width: 150, height: 150))
emojiLabel.text = wordToLookup
emojiLabel.font = UIFont.systemFont(ofSize: 100.0)

containerView.addSubview(emojiLabel)

meaning = emojiDict[wordToLookup]
let meaningLabel = UILabel(frame: CGRect(x: 110, y: 100, width: 150, height: 150))
meaningLabel.text = meaning
meaningLabel.font = UIFont.systemFont(ofSize: 30.0)
meaningLabel.textColor = UIColor.white
containerView.addSubview(meaningLabel)


//var testVarValue
// error: type annotation missing in pattern
//let testLetValue
// error: type annotation missing in pattern

var jobTitle: String?
jobTitle = "iOS Developer"

//var jobMessage = "Your job title is " + jobTitle
// error: value of optional type 'String?' must be unwrapped to a value of type 'String'

// Forced Unwrapping
if jobTitle != nil {
var jobMessage = "Your job title is " + jobTitle!
}


//jobTitle = nil
//var jobMessage = "Your job title is " + jobTitle!
// error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0).


// Optional Binding
if let jotTitleWithValue = jobTitle {
var message = "Your job title is " + jotTitleWithValue
}

if let jobTitle = jobTitle {
var message = "Your job title is " + jobTitle
}

jobTitle = nil;
if let jobTitle = jobTitle {
// never execute
var message = "Your job title is " + jobTitle
}