2、 字符串中直接引入代码
let a = 1
let b = 2
let say = "the number is \(a + b)" // "the number is 3"
三、数组、字典 都能够直接使用 [] 声明
1、数组
2、字典
var man = [
"like": "apple",
"name": "Tom",
]
man["iphone"] = "5s" // 添加字段
man["like"] = "mac" // 改动字段
man // ["iphone": "5s", "like": "mac", "name": "Tom"]
四、控制语句
1、if 不能够直接使用一个变量或者常量来作为条件,必须是推断语句
let a = 12
if a > 0 {
// do something
} else {
// do something
}
假设你写成 if a {} ,就会报错,不能直接使用变量或常量推断bool
2、for
for i in 0..3 {
// i = 0, 1, 2
}
for var i = 0; i < 3; ++i {
// i = 0, 1, 2
}
// 数组
let scores = [1, 2, 3, 4, 5]
for score in scores {
// score
}
// 字典
let numsDic = [
"a": [2, 3, 5, 7, 11, 13],
"b": [1, 1, 2, 3, 5, 8],
"c": [1, 4, 9, 16, 25],
]
for (key, numbers) in numsDic {
for number in numbers {
// number
}
}
3、while
var n = 2
while n < 100 {
n = n * 2
}
var m = 2
do {
m = m * 2
} while m < 100
请发表评论