Swift --- 标准库源码分析

Swift源码简介

  • Swift于2015年正式开源,github地址:https://github.com/apple/swift
  • 几个可能会经常看的目录
  1. docs:一些文档
  2. stdlib:Swift源码
  3. lib:C++源码
  4. 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)])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值