随着项目愈来愈大,编译时长也会愈来愈长。那么编译的优化就必不可少了。git
在 xcode 的编译 log 能够查看时长,也能够查看总时长,每个 文件的编译时长。github
对于 swift 来讲,编译耗时的主要就是类型检查 在 xcode => build settings => Other Swift Flags
添加下面设置,能够看到 swift 的表达式和函数的 类型检查的时长。并给出警告。express
-Xfrontend -warn-long-function-bodies=100 (100 means 100ms here, you should experiment with this value depending on your computer speed and project)
-Xfrontend -warn-long-expression-type-checking=100
复制代码
后面的 100 单位是毫秒,超过这个值,就会给出警告。swift
在终端执行:xcode
defaults write com.apple.dt.Xcode ShowBuildOperationDuration -bool YES
复制代码
重启 xcode,编译能够看到总耗时 bash
能够设置在 debug
不生成 dSYM
,只在 release
生成。闭包
能够把 pod 等第三方库先打包成 .a 文件,而后再放入项目里面,这样 pod 的第三方库就编译时间就会减小。可是这样一来 调试就不方便了,因此这是个取舍问题。app
oc 的优化,重点在于减小无效引用,对编译时长的优化提高很是明显。 经过 log 看哪些文件编译时间比较长的文件,进行优化。frontend
快捷键⌘ + 8
模块化
一、检查 pch 文件,删除用的很少的引用。
.h
文件尽可能少写引用。引用尽可能写在 .m
文件里。能够在编译 log 中看有哪些文件编译时间比较长的,大部分均可以经过 优化引用来解决。
项目时间长了以后,由于功能变化,有些类可能已经不用了,可是引用还在,这个必定要删掉。
通过优化,编译时长减小了2-3 分钟。
根据以前说法,把 swift 的文件合并成一个,能够大大减小编译时长。可是 xcode10 已经没用这个选项了。 xcode10 已经给出了优化的选择,咱们主要选择正确的选项就能够,根据本身的需求,通常 debug 选择 增量编译,Adhoc 和 Release 能够选择模块编译
原理能够参考: medium.com/rocket-fuel…
swift 设置优化完以后,主要就是代码的优化了。swift 耗时的主要就是类型检查,那么代码优化的重点就是 检查哪些表达式、函数 类型检查占用太长时间。
能够用工具 Swift BuildTimeAnalyzer tool 来查看每个文件的编译时长。
在网上查了各类说法,下面是 swift 代码优化的重点,也确实有用。
不推荐
private(set) lazy var chartViewColors: [UIColor] = [
self.chartColor,
UIColor(red: 86/255, green: 84/255, blue: 124/255, alpha: 1),
UIColor(red: 80/255, green: 88/255, blue: 92/255, alpha: 1),
UIColor(red: 126/255, green: 191/255, blue: 189/255, alpha: 1),
UIColor(red: 161/255, green: 77/255, blue: 63/255, alpha: 1),
UIColor(red: 235/255, green: 185/255, blue: 120/255, alpha: 1),
UIColor(red: 100/255, green: 126/255, blue: 159/255, alpha: 1),
UIColor(red: 160/255, green: 209/255, blue: 109/255, alpha: 1),
self.backgroundGradientView.upperColor
]
复制代码
推荐:
// Cumulative build time: 56.3ms
private(set) lazy var chartViewColors: [UIColor] = self.createChartViewColors()
// Build time: 6.2ms
private func createChartViewColors() -> [UIColor] {
return [
chartColor,
UIColor(red: 86/255, green: 84/255, blue: 124/255, alpha: 1),
UIColor(red: 80/255, green: 88/255, blue: 92/255, alpha: 1),
UIColor(red: 126/255, green: 191/255, blue: 189/255, alpha: 1),
UIColor(red: 161/255, green: 77/255, blue: 63/255, alpha: 1),
UIColor(red: 235/255, green: 185/255, blue: 120/255, alpha: 1),
UIColor(red: 100/255, green: 126/255, blue: 159/255, alpha: 1),
UIColor(red: 160/255, green: 209/255, blue: 109/255, alpha: 1),
backgroundGradientView.upperColor
]
}
复制代码
Array
,Dictionary
集合类型 须要指明类型,而且不要用 +
来合并两个集合
?:
// Before
return someValue > 3 ? someValue - 2 : someValue + 2
// After
if someValue > 3 {
return someValue - 2
} else {
return someValue + 2
}
复制代码
??
return CGSize(width: size.width + (rightView?.bounds.width ?? 0) + (leftView?.bounds.width ?? 0) + 22, height: bounds.height)
// After
var padding: CGFloat = 22
if let rightView = rightView {
padding += rightView.bounds.width
}
if let leftView = leftView {
padding += leftView.bounds.width
}
return CGSize(width: size.width + padding, height: bounds.height)
复制代码
?:
和 ??
均可以经过 条件绑定来解决。
不要在一个表达式中既作判断,又作计算
if number == 60 * 60 {
// ...
}
// After
let number: Double = 60 * 60
if number == 3600 {
// ...
}
复制代码
代码优化能够参这里