入门文档
- Guide Book:https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html
- Swift Feature: https://developer.apple.com/swift/
问题记录
- Swift属于面向对象还是面向函数编程?
- Swift开源吗
开源,代码可在github上下载:https://github.com/apple/swift.git
./swift/utils/update-checkout
Swift Feature
- 值类型
And Swift heavily leverages value types, especially for commonly used types like Arrays and Dictionaries. This means that when you make a copy of something with that type, you know it won’t be modified elsewhere.
- nil
Another safety feature is that by default Swift objects can never be nil. In fact, the Swift compiler will stop you from trying to make or use a nil object with a compile-time error. This makes writing code much cleaner and safer, and prevents a huge category of runtime crashes in your apps. However, there are cases where nil is valid and appropriate. For these situations Swift has an innovative feature known as optionals. An optional may contain nil, but Swift syntax forces you to safely deal with it using the ? syntax to indicate to the compiler you understand the behavior and will handle it safely.
- 语句结尾不需要分号;
- 提供了便捷的方式获取一个类型的最大最小值:UInt8.max
Swift 5.3 基础
赋值
let maximumNumberOfLoginAttempts = 10
var currentLoginAttempt = 0
var x = 0.0, y = 0.0, z = 0.0
变量在第一次赋值的时候会确定类型,不能将类型不匹配的值赋值给变量:
类型
- Int 对应OC中的NSInteger
- UInt 对应OC中的NSUInteger
- Double
- Float
默认类型
- 整数默认类型:Int
- 浮点数默认类型:Double
类型别名
//使用typealias关键字定义一个类型别名
typealias AudioSample = UInt16
- Bool 取值true;false
- 判断条件只能传入Bool型,Int不能转Bool
元组 Turples
let http404Error = (404, "Not Found")
// http404Error is of type (Int, String), and equals (404, "Not Found")
通过元组可以方便地构建一个数据结构.
元组拆解:
let (statusCode, statusMessage) = http404Error
print("The status code is \(statusCode)")
// Prints "The status code is 404"
print("The status message is \(statusMessage)")
// Prints "The status message is Not Found"
let (justTheStatusCode, _) = http404Error
print("The status code is \(justTheStatusCode)")
// Prints "The status code is 404"
使用点语法获取元组中的值:
print("The status code is \(http404Error.0)")
// Prints "The status code is 404"
print("The status message is \(http404Error.1)")
// Prints "The status message is Not Found"
元素命名:
let http200Status = (statusCode: 200, description: "OK")
print("The status code is \(http200Status.statusCode)")
// Prints "The status code is 200"
print("The status message is \(http200Status.description)")
// Prints "The status message is OK"
可选值 Optionals
Optional的变量意味着可能是nil,也可能是一个非nil的值,通过加上"!"后缀可以解除Optional,这样会“指示”编译器:该变量不会是nil.
编程规范:Optional必须拆包之后才能使用
- 默认情况下不能将nil赋值给一个变量
- 变量需要定义为Optional,才能赋值为nil:
- 使用 ! 解除变量的Optional
name 类型为:String?
name! 类型为: String
Optional Binding,可以用来简化 if / while 语句:
- 通过Optional Binding,可以方便地判断一个量是否为空,如果非空,则生成一个局部的“非空”常量
Optional的隐式解包
有时候通过代码结构可以断定一个optional是非空的,那么移除它的optional属性是有必要的,这个移除optional属性的操作称为解包。
苹果文档中提到了几个概念:
- non-optional value,eg:String
- optiaonal value,eg:String?
- implicitly unwrapped optional value,eg:String!,本质上仍然属于optional value
//类型后加感叹号,代表它是一个implicitly unwrapped optional value