Xcode 10.2 Build failed:Command CompileSwiftSources failed with a nonzero exit code
Xcode 10.2
今天早上,苹果推出了 Xcode 10.2 更新。如果你更新了,可能会遭遇一个未分类的 error:
Command CompileSwiftSources failed with a nonzero exit code
完全不知道编译失败的原因。
实际上苹果已经在 Release Note 里说过这事了:
Compilation might fail without displaying the errors responsible for the failure. For example, you might see the message “Command CompileSwiftSources failed with a nonzero exit code” without an accompanying failure reason. (43033749) Workaround: Disable batch mode by adding a user-defined build setting named SWIFT_ENABLE_BATCH_MODE and set it to NO.
具体操作
打开你的 Target 的 Build Settings:
Build Settings
点击加号,添加一个 User-Defined Setting:
添加 User-Defined Setting
命名为 SWIFT_ENABLE_BATCH_MODE,设置为 NO:
设置 SWIFT_ENABLE_BATCH_MODE
设置完,再编译就能显示确切的 Error 位置。
常见的第三方库编译问题
Xcode 10.2 进行了更严格的代码检查,引入了大量的新 Warning 和 Error(喜闻乐见),各个第三方库的代码规范程度也可见一斑:
![第三方库](/Users/imoe/Documents/Articles/iOS Amazing/assets/第三方库.png)
可以看到,Moya 和猫神的 Kingfisher 都是不错的。当然了,这其实没什么参考价值的。
不过 WCDB 则干脆无法编译了。你可以进行以下修改来通过编译:
注意,建议将对第三方库的修改单独包含在一次 commit 中,不要和自己项目的修改混在一起。
Tencent WCDB 适配
如果你使用了腾讯开源的 WCDB,需要做以下修改:
- 在
WCDB.swift/CodableType.swift
中:
// 第 244 行:
// 原来:
extension Array: ColumnCodable, ColumnCodableBase where Element: Codable {
// 修改后:
extension Array: ColumnCodable where Element: Codable {
// 第 262 行:
// 原来:
extension Dictionary: ColumnCodable, ColumnCodableBase where Key: Codable, Value: Codable {
// 修改后:
extension Dictionary: ColumnCodable where Key: Codable, Value: Codable {
// 第 280 行:
// 原来:
extension Set: ColumnCodable, ColumnCodableBase where Element: Codable {
// 修改后:
extension Set: ColumnCodable where Element: Codable {
分析:
在 WCDB.swift/ColumnCodable.swift
中,ColumnCodable
的定义如下:
public typealias ColumnCodable = ColumnCodableBase & ColumnEncodable & ColumnDecodable
而 ColumnEncodable
遵守了 ColumnCodableBase
:
public protocol ColumnEncodable: Encodable, ColumnCodableBase {
func archivedValue() -> FundamentalValue
}
所以修改前的代码要求类型同时遵从 ColumnCodable
和 ColumnCodableBase
协议,是一次重复遵从:
// 第 280 行:
// 原来:
extension Set: ColumnCodable, ColumnCodableBase where Element: Codable {
2. 在 WCDB.swift/Tokenize.swift 中:
// 第 111 行:
// 原来:
bytes = Int32(strlen(pInput))
// 修改后:
if let pInput = pInput {
bytes = Int32(strlen(pInput))
}
这没什么可说,是 pInput
需要 unwrap
。