目前Swift的穩定版本是5.4
https://docs.swift.org/swift-book/index.html
XCode新建項目的build-setting中Swift Language Version 設置為 Swift5
近期在項目開發中使用到了swift,在本地可以正常編譯運行項目,但是在jenkins打包的時候拋出如下錯誤。
分析總結這個問題,避免后面的同學被同一個問題困擾。
參考XCode更新文檔
https://developer.apple.com/xcode/whats-new/
發現從XCode12以后才開始引入Swift5.3及以上版本
Swift5.3更新部分
- Added information about multiple trailing closures to the Trailing Closures section, and added information about how trailing closures are matched to parameters to the Function Call Expression section.
what's problem ?
The following build commands failed:
CompileSwift normal armv7 xxx.swift
CompileSwiftSources normal arm64 com.apple.xcode.tools.swift.compiler
CompileSwift normal arm64 xxx.swift
why problem ?
xxx.swift:24:10: error: consecutive statements on a line must be separated by ';'
} completion: { _ in
^
;
xxx.swift:24:21: error: expected expression
} completion: { _ in
^
xxx.swift:24:11: error: use of unresolved identifier 'completion'
} completion: { _ in
^~~~~~~~~~
xxx.swift:24:23: error: unable to infer closure type in the current context
} completion: { _ in
^~~~~~
xxx.swift:24:23: error: closure expression is unused
} completion: { _ in
How fix ?
// xcode12 and above
UIView.animate(withDuration: 0.3) {
// animation actions
} completion: { _ in
// some finished actions
}
// below xcode12
UIView.animate(withDuration: 0.3, animations: {
// animation actions
}, completion: { _ in
// some finished actions
})
不同版本XCode編譯器在編譯swift文件,做語法檢查的時候,檢測到該語法不支持。爆出這樣的錯誤