Xcode 10.2 編譯失敗,如何對敵?
阿新 • • 發佈:2020-12-26
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
。