【ios】swift教程
学习swift地址
https://docs.swift.org/swift-book/GuidedTour/GuidedTour.html
和其他语言比起来,语法层次确实有些不一样的地方。
在wwdc2019上看到了苹果公司关于swiftui的介绍,感觉这就是未来。
Parentheses()
brackets[]
braces{}
Swift编程语言简介
Swift命令行操作
1 | Swift defines away large classes of common programming errors by adopting modern programming patterns: |
2 | |
3 | Variables are always initialized before use. |
4 | Array indices are checked for out-of-bounds errors. |
5 | Integers are checked for overflow. |
6 | Optionals ensure that nil values are handled explicitly. |
7 | Memory is managed automatically. |
8 | Error handling allows controlled recovery from unexpected failures. |
A swift tour
下面的内容如果你有不理解的请不要太过于悲观,我们将会在以后继续学习
Simple Values
let 定义常量
var 定义变量
Control Flow
Use if and switch to make conditionals, and use for-in, while, and repeat-while to make loops. Parentheses around the condition or loop variable are optional. Braces around the body are required.
1 | if true { |
2 | print(“this is true”) |
3 | } else{ |
4 | print(“this is not true”) |
5 | } |
6 | var tmp = “” |
7 | switch tmp{ |
8 | case “1”: |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | } |
15 | |
16 | for i in [1, 3, 5,7,9]{ |
17 | |
18 | } |
19 | for i in 1..<5{ |
20 | print(i) |
21 | } |
22 | for i in 1…5{ |
23 | print(i) |
24 | } |
1 | while condition { |
2 | |
3 | } |
1 | repeat { |
2 | print(“至少执行一次”) |
3 | }while 2 < 1 |
4 |
函数和闭包
By default, functions use their parameter names as labels for their arguments. Write a custom argument label before the parameter name, or write _ to use no argument label.
1 | func greet(person: String, day: String) -> String { |
2 | return “Hello \(person), today is \(day).” |
3 | } |
4 | greet(person: “Bob”, day: “Tuesday”) |
对象和类
类
实例话一个类
Enumerations and Structures
Protocols and Extensions
Error Handling
异常捕获