1. 补充: 如何获取一个堆空间对象的地址指针?(多练习,需牢记,拿到指针可以为所欲为
)

获取堆空间对象的地址指针
一、字面量
1. 什么是字面量(Literal)? Swift 自带的绝大部分类型,都支持直接通过字面量进行初始化吗?

字面量基本介绍
2. 思考:为什么 Swift 中自带类型能够通过字面量初始化
?

字面量协议
3. 实践:如何让 Int 变量能够使用字面量 true 来赋值表示 1
?
- 让
Int
扩展 ExpressibleByBooleanLiteral
协议的 init
方法
extension Int: ExpressibleByBooleanLiteral {
public init(booleanLiteral value: BooleanLiteralType) { self = value ? 1 : 0 }
}
// 原本会报错的代码
var num: Int = true
print(num) // 1
4. 实践:如何使用字面量初始化 Person 对象?
class Person: ExpressibleByIntegerLiteral {
var age = 0
init(age: Int) {
self.age = age
}
required init(integerLiteral value: IntegerLiteralType) {
age = value
}
}
var person:Person = Person(age: 10)
print(person.age) // 10
// 用字面量初始化 Person
var person2:Person = 20
print(person2.age) // 20
二、模式匹配
1. 模式(Pattern)基本介绍

image.png
2. 通配符模式(Wildcard Pattern),包含两种 _
和 _?
请问这两种在模式中有什么不同?
-
_
匹配任何值
-
_?
匹配非 nil 值
image.png
3. 元组模式(Tuple Pattern)

image.png
4. 枚举 case 模式(Enumeration Case Pattern)

image.png
- 总结:无论
if case
还是 for case
,把它们想象成 witch case
来理解,就非常好理解。
5. is 和 as 在类型转换模式(Type-Casting Pattern)
的区别?
- is 一般仅仅用于
判断类型
- as 用于
判断类型 + 类型转换

image.png
6. 什么是表达式模式(Expression Pattern)

image.png
7. 如何自定义表达式模式?

image.png
8. where 一般在 Swift 中运用,有 5 处,备用知识

image.png
三、OC 到 Swift
1. MARK、TODO、FIXME 用于干什么?

image.png
2. 条件编译能判断的一些条件?(了解)

image.png
3. 如何让网络请求在 debug 模式下测试服务器,在 release 模式下走正式服务器?
let baseHTTP: String
#if DEBUG
baseHTTP = "http://develop.com"
#else
baseHTTP = "http://product.com"
#endif
print(baseHTTP)
4. 在 Swift 中如何让 Log 仅仅在 debug 模式下打印,在 release 模式下不打印?
func log<T>(_ value: T){
#if DEBUG
print(value)
#endif
}
- 使用
泛型
,能打印任何类型
-
print(value)
只会在 debug
模式执行
- 在
release
模式下 log(:)
不执行任何代码,会被编译器优化,直接不调用,所以不影响性能
5. 如何构建一个打印方法,仅在 debug 模式打印,并且会打印出①函数所在文件位置②行数③方法名?
func log<T>(_ value: T,
file: NSString = #file,
line: Int = #line,
function: String = #function){
#if DEBUG
print("file:\(file.lastPathComponent) line:\(line) function:\(function) log:\(value)")
#endif
}
6. 系统版本检查。(了解)

image.png
7. API 可用性说明

image.png
网友评论