let doubleDecimal =15.0//十进制, 等价于1.25e2,0.0125等价于1.25e-2 let doubleHexadecimal1 =0xFp2// 十六进制, 意味着25×2^2,相当于十进制的60 let doubleHexadecimal2 =0xFp-2// 十六进制, 意味着25×2^-2,相当于十进制的3.75
数组
1
let array = [1, 3, 5, 7, 9]
字典
1
let dictionary = ["age": 18, "height": 168, "weight": 120]
类型转换
整数转换
1 2 3
let int1: UInt16=2_000 let int2: UInt8=1 let int3 = int1 +UInt16(int2)
整数、浮点数转换
1 2 3 4
let int =3 let double =0.14 let pi =Double(int) + double let intPi =Int(pi)
字面量相加
1 2
// 字面量可以直接相加,欣慰数字字面量本身没有明确类型 let result =3+0.14
元组
1 2 3 4 5 6 7 8 9 10
let http404Error = (404, "Not Found") print("The status code is \(http404Error.0)")
let (statusCode, statusMessage) = http404Error print("The status code is \(statusCode)")
let (justTheStatusCode, _) = http404Error
let http200Error = (statusCode: 200, description: "OK") print("The status code is \(http200Error.statusCode)")
流程控制
if-else
1 2 3 4 5 6 7 8 9 10
let age =4 if age >=22 { print("Get married") } elseif age >=18 { print("Being a adult") } elseif age >=7 { print("Go to school") } else { print("Just a child") }
if后面的条件可以省略小括号
条件后边的大括号不可以省略
if后边的条件智能是Bool类型
1 2 3 4 5
// 错误代码 let age =1 if age {
}
while
1 2 3 4 5
var num =5 where num >5 { print("num is \(num)") num -=1 } //打印了5次
1 2 3 4 5
var num =-1 repeat { print("num is \(num)") num -=1 } while num >0//打印了1次
repeat-while 相当于C语言的do-while
这里不用num–,是因为从Swift3开始,去除了自增(++),自减(–)运算符
for
闭区间运算符:a…b, a <= 取值 <= b
1 2 3 4 5
let names = ["Anna", "Alex", "Brian", "Jack"] for i in0...3 { print(names[i]) } // Anna, Alex, Brian, Jack
1 2 3 4 5 6
let names = ["Anna", "Alex", "Brian", "Jack"] let range =1...3 for i in range { print(names[i]) } // Alex, Brian, Jack
1 2 3 4 5 6 7
let names = ["Anna", "Alex", "Brian", "Jack"] let a =1 let b =2 for i in a...b { print(name[i]) } // Alex, Brian
1 2 3 4 5 6
// i默认是let, 如果有需要可以声明为var forvar i in1...3 { i +=5 print(i) } // 6 7 8
1 2 3 4
for_ i in1...3 { print("for") } // 打印了三次
半开区间运算符:a..<b, a <= 取值 < b
1 2 3 4
for i in1..<5 { print(i) } // 1 2 3 4
区间运算符用在数组上
1 2 3 4 5
let names = ["Anna", "Alex", "Brian", "Jack"] for name in names[0...3] { print(name) } // Anna, Alex, Brian, Jack
单侧区间 让区间朝着一个方向尽可能的远
1 2 3 4 5
let names = ["Anna", "Alex", "Brian", "Jack"] for name in names[2...] { print(name) } // Brian, Jack
1 2 3 4
for name in names[...2] { print(name) } // Anna, Alex, Brian
1 2 3 4
for name in names[..<2] { print(name) } // Anna, Alex
1 2 3 4
let range =...5 range.contains(7) // false range.contains(4) // true range.contains(7-3) // true
区间类型
1 2 3
let range1: ClosedRange<Int> =1...3 let range2: Range<Int> =1..<3 let range3: PartialRangeThrough<Int> =...5
// \0到~囊括了所有可能要用到的ASCII字符 let characterRange: ClosedRange<Character> ="\0"..."~" characterRange.contains("G") // true
带间隔的区间值
1 2 3 4 5 6
let hours =11 let hourInterval =2 // tickMark的取值:从4开始,累加2,不超过11 for tickMark instride(from: 4, through: hours, by: hourInterval) { print(tickMark) } // 4 6 8 10
switch
case、default后面不能写大括号{}
1 2 3 4 5 6 7 8 9 10 11
var number =1 switch number { case1: print("number is 1") breakcase2: print("number is 2") break default: print("number is other") break } // number is 1
默认可以不写break,并不会贯穿到后面的条件
1 2 3 4 5 6 7 8 9
var number =1 switch number { case1: print("number is 1") case2: print("number is 2") default: print("number is other") } // number is 1
使用fallthrough可以实现贯穿效果
1 2 3 4 5 6 7 8 9 10 11 12
var number =1 switch number { case1: print("number is 1") fallthrough case2: print("number is 2") default: print("number is other") } // number is 1 // number is 2
switch必须要保证能处理所有情况
1 2 3 4 5 6 7 8
// 错误示范,编译器会报错;应该加上default var number =1 switch number { case1: print("number is 1") case2: print("number is 2") }
case、default后面至少要有一条语句
如果不想做任何事,加个break即可
1 2 3 4 5 6 7 8 9
var number =1 switch number { case1: print("number is 1") case2: print("number is 2") default: break }
如果能保证已处理所有情况,也可以不必使用default
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
enum Answer { case right, wrong } let answer = Answer.right switch answer { case Answer.right: print("right") case Answer.wrong: print("wrong") } // 由于已确定answer是Ansewer类型,因此可以省略Answer switch answer { case .right: print("right") case .wrong: print("wrong") }
switch也支持Character、String类型
1 2 3 4 5 6 7 8 9 10
let string ="Jack" switch string { case"Jack": fallthrough case"Rose": print("Right person") default: break } // Right person
1 2 3 4 5 6 7 8
let character: Character="a" switch character { case"a", "A": print("The letter A") default: print("Not the letter A") } // The letter A
1 2 3 4 5 6 7
switch string { case"Jack", "Rose": print("Right person") default: break } // Right person
let point = (1, 1) switch point { case (0, 0): print("the origin") case (_, 0): print("on the x-axis") case (0, _): print("on the y-axis") case (-2...2, -2...2): print("inside the box") default: print("outside of the box") } // inside the box
值绑定 必要时 let也可以改为var
1 2 3 4 5 6 7 8 9 10
let point = (2, 0) switch point { case (let x, 0): print("on the x-axis with an x value of \(x)") case (0, let y): print("on the y-axis with a y value of \(y)") caselet (x, y): print("somewhere else at (\(x), \(y))") } // on the x-axis with an x value of 2
where
1 2 3 4 5 6 7 8 9 10
let point = (1, -1) switch point { caselet (x, y) where x == y: print("on the line x == y") caselet (x, y) where x ==-y: print("on the line x == -y") caselet (x, y): print("(\(x), \(y)) is just some arbitrary point") } // on the line x == -y
1 2 3 4 5 6 7
// 将所有正数加起来 var numbers = [10, 20, -10, -20, 30, -30] var sum =0 for num in numbers where num >0 { // 使用where来过滤num sum += num } print(sum) // 60
标签语句;指定跳出的循环层级
1 2 3 4 5 6 7 8 9 10 11
outer: for i in1...4 { for k in1...4 { if k ==3 { continue outer } if i ==3 { break outer } print("i == \(i), k == \(k)") } }
/// - Parameters: /// - items: Zero or more items to print. /// - separator: A string to print between each item. The default is a single space (`" "`). /// - terminator: The string to print after all items have been printed. The /// default is a newline (`"\n"`). publicfuncprint(_items: Any..., separator: String=" ", terminator: String="\n")