目前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文件,做语法检查的时候,检测到该语法不支持。爆出这样的错误
CompileSwiftSources normal arm64 com.apple.xcode.tools.swift.compiler