Swift源码简介
- Swift于2015年正式开源,github地址:https://github.com/apple/swift
- 几个可能会经常看的目录
- docs:一些文档
- stdlib:Swift源码
- lib:C++源码
- include:C++头文件
- 标准库源码位置:https://github.com/apple/swift/tree/master/stdlib/public/core
Array分析
-
map、filter
-
https://github.com/apple/swift/blob/master/stdlib/public/core/Sequence.swift
-
flatMap、compactMap、reduce
-
https://github.com/apple/swift/blob/master/stdlib/public/core/SequenceAlgorithms.swift
Substring分析
- append、lowercased、uppercased
- https://github.com/apple/swift/blob/master/stdlib/public/core/Substring.swift
Optional分析
- map、flatMap、==、??
- https://github.com/apple/swift/blob/master/stdlib/public/core/Optional.swift
- Metadata分析
- https://github.com/apple/swift/blob/master/docs/ABI/TypeMetadata.rst
反射
-
反射是变成语言中一项强大的能力,比如Java语言的反射机制
-
对于任意一个类型,都能够动态获取这个类的所有属性和方法信息
-
对于任意一个实例,都能够动态调用它的任意方法和属性
-
Swift的反射机制目前还比较弱,通过Mirror类型来提供简单的反射功能
struct Person {
var age: Int = 0
var name: String = ""
}
let mirror = Mirror(reflecting: Person(age: 10, name: "Jack"))
// struct
print(mirror.displayStyle!)
// Person
print(mirror.subjectType)
// nil
print(mirror.superclassMirror as Any)
// age 10
// name Jack
for case let (label?, value) in mirror.children {
print(label, value)
}
常用Swift第三方库
- 网络请求:https://github.com/Alamofire/Alamofire
- 图片下载:https://github.com/onevcat/Kingfisher
- JSON访问:https://github.com/SwiftyJSON/SwiftyJSON
- JSON-Model转换:https://github.com/kakaopensource/KakaJSON
Kingfisher注意点
- Kingfisher默认不支持WebP格式的图片,需要额外安装KingfisherWebP
pod 'KingfisherWebP'
iconView.kf.setImage(with:URL(string: user.thumb),
options:[.processor(WebPProcessor.default),
.cacheSerializer(WebPSerializer.default)])