Swift从入门到精通第一篇-基础语法和流程控制
前言
学习任何一门语言都是从HelloWorld !开始。
print("Hello World!")
- 不用编写main函数,将全局范围内的首名可执行代码作为程序入口
- 可以省略
;
var
定义变量let
定义常量
注释
- // 单行注释
- 多行注释
/*
外层
/* 多行注释内部可以嵌套 */
外层
*/
常量&变量
-
常量:只能赋值一次,它的值不要求在编译时期确定,但使用之前必须赋值一次
let let1 = 10 let let2: Int let2 = 10
-
常量和变量未初始化,均不能使用
let let3: Int var var1: Int print(let3) // Constant 'age' used before being initialized print(var1) // Variable 'var1' used before being initialized
标识符
- 标识符(Swift强大之处)(比如常量名、变量名、函数名)几乎可以使用任何字符
- 标识符不能以数字开头,不能包含空白字符、制表符、箭头等特殊字符
常见数据类型
引用类型(reference type) | 类(class) |
值类型(value type) | 枚举(enum)Optional |
结构体(struct) Bool Int Float Double Character String Array Dictionary Set |
字面量
- 布尔
let bool = true // false
- 字符串
let string = "Swift"
- 整数
let intDecimal = 17 // 十进制 let intBinary = 0b10001 // 二进制 let intOct = 0o21 // 八进制 let intHex = 0x11 // 十六进制
- 字符
let character: Character = "🎉"
- 浮点数
let doubleDecimal = 125.0 // 十进制 == 1.25e2 let doubleDecimal2 = 0.0125 // 十进制 == 1.25e-2 let doubleHex = 0xFp2 // == 15 * 2^2 == 十进制 60 let doubleHex2 = 0xFp-2 // == 15 * 2^-2 == 十进制 3.75 // 12.1875 十进制 12.1875 十六进制 0xC.3p0 (12 + 0.3/16 * 2^0) // 整数和浮点数可以添加额外的零或者添加下划线g增强可读性,如 1_000_000_000
- 数组
let array = [1, 2, 3, 4]
- 字典
let dict = ["Chinese" : 100, "Math": 100, "Computer": 101]
- 元组
let http404Error = (statusCode: 404, statusMessage: "Not Found") print("The status code is \(http404Error.0)") print("The status code is \(http404Error.statusCode)")
流程控制
-
if-else if后面的条件只能是Bool类型,后面的小括号d可以省略,条件后面的大括号不能省略;
let let4 = 10 if let4 < 5 { print("let4 < 5") }else if(let4 < 7){ print("5 <= let4 < 7") }else{ print("else") }
-
while
var num = 5 while num > 0 { print("num is \(num)") num -= 1 // 没有num--语法 } // print 5 次 var num2 = -1 repeat { print("repeat-while") }while num2 > 0 // print 1 次 == C的do-while
-
闭区间运算符 a...b == a <= var <= b
let names = ["Anna", "Alex", "Brian", "Jack"] for i in 0...3 { // i 默认是let print(names[i]) } // Anna Alex Brian Jack for var i in 1...3 { // 要修改用var i += 5 print(i) } // 6 7 8
-
半开区间运算符 x..<'b' == a <= var < b
for i in 1..<4 { print(i) } // 1 2 3
-
区间运算符用在数组中
for name in names[0...3] { print(name) } // Anna Alex Brian Jack
-
单侧区间
for name in names[2...] { print(name) } // Brian Jack for name in names[..<2] { print(name) } // Anna Alex
-
区间类型
let range = ...5 range.contains(7) // false range.contains(4) // true range.contains(-3) // true let range1: ClosedRange<Int> = 1...3 let range2: Range<Int> = 1..<3 let range3: PartialRangeThrough<Int> = ...5
-
字符串、字符也能使用区间运算符,但默认不能d用在for-in中
let stringRange = "cc"..."ff" // ClosedRange<String> stringRange.contains("cb") // false stringRange.contains("cd") // true
-
带间隔的区间值
let hours = 10 let hourInterval = 2 // tickMark的取值:从4开始,累加2, 不超过10 for tickMark in stride(from: 4, through: hours, by: hourInterval) { print(tickMark) } // 4 6 8 10
-
switch case、default后面不能写大括号{},且case、default后面至少要有一条语句, 默认可以不写break, 并不会贯穿后面的条件,fallthrough可以实现贯穿效果
var number = 1 switch number { case 1: print("number is 1") fallthrough case 2, 3: print(2) print(3) break default: break }
-
区间匹配、元组匹配
let count = 62 switch count { case 0: print("none") case 1..<5: print("a few") case 5..<100: print("dozens of") case 100..<1000: print("hundreds of") default: print("many") } 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
let point2 = (2, 0) switch point2 { 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 an x value of \(y)") case (0, _): print("on the y-axis") case let (x, y): print("somewhere else at (\(x), \(y))") } // on the x-axis with an x value of
-
where
let point3 = (1, -1) switch point3 { case let (x, y) where x == y: print("on the line x == y") case let (x, y) where x == -y: print("on the line x == -y") case let (x, y): print("(\(x), \(y)) other point") } // on the line x == -y
-
标签语句
outer: for i in 1...4 { for k in 1...4 { if k == 3 { continue outer } if i == 3 { break outer } print("(i = \(i), k = \(k))") } } // (1, 1) (1, 2) (2, 1) (2, 2)