在 Xcode/Cocoapods 项目中更新 swift 版本的正确方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/55678321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the correct way to update swift version in Xcode/Cocoapods project?
提问by Lena V
I have an Xcode project, with cocoapods (RealmSwift) that was written in Swift 4.2.
我有一个 Xcode 项目,带有用 Swift 4.2 编写的 cocoapods (RealmSwift)。
I have since updated Xcode to 10.2, and am now being prompted to convert my code (and the RealmSwift code) to Swift 5, and a load of warnings are being generated for Realm Swift (i.e. 'public' modifier is redundant for instance method declared in a public extension).
我已经将 Xcode 更新到 10.2,现在我被提示将我的代码(和 RealmSwift 代码)转换为 Swift 5,并且正在为 Realm Swift 生成大量警告(即“public”修饰符对于声明的实例方法是多余的在公共扩展中)。
What is the correct way to update the Cocoapods code? Do I need to specify/upgrade to a newer version of RealmSwift (and how would I know it was Swift 5 compatible?), or is there a recommended way of suppressing the warnings if I don't want to upgrade to Swift 5?
更新 Cocoapods 代码的正确方法是什么?我是否需要指定/升级到较新版本的 RealmSwift(我怎么知道它与 Swift 5 兼容?),或者如果我不想升级到 Swift 5,是否有推荐的方法来抑制警告?
Any pointers to some documentation on the best way to handle xcode/swift/cocoapods updates would be appreciated.
任何有关处理 xcode/swift/cocoapods 更新的最佳方式的文档的任何指针都将不胜感激。
回答by denis_lor
When updating Xcode version you should just be sure you don't end up in a situation where you can no longer work.
更新 Xcode 版本时,您应该确保不会出现无法再工作的情况。
Instead of updating Xcode you could just install two version of Xcode on the same mac and use both of them by trying out the new version.
您可以在同一台 Mac 上安装两个版本的 Xcode,并通过试用新版本来使用它们,而不是更新 Xcode。
For now, you can go back to your previous Xcodeby downloading the previous version here https://developer.apple.com/download/more, extract the archive you will download and copy the App file to your Applications folder in macOS.
现在,您可以通过在https://developer.apple.com/download/more下载以前的版本返回到您以前的 Xcode,解压缩您将下载的存档并将 App 文件复制到 macOS 中的应用程序文件夹。
What I would suggest as a solution before you start upgrading everything, as you don't know how all the new pod updates will work with your code implementation, is to delete the Xcode 10.2 and go back install 10.1 or 10.0 (can download them from the previous link).
在开始升级所有内容之前,我建议的解决方案是删除 Xcode 10.2 并返回安装 10.1 或 10.0(可以从上一个链接)。
For later on, when your project builds fine and it's stableand you would like to try to upgrade everything (Xcode, Swift and PODs), you should first of all check if your project build with your current Swift version (which Swift version is your project currently set on you can find under Target > Build Settings > Swift Language Version).
稍后,当您的项目构建良好且稳定并且您想尝试升级所有内容(Xcode、Swift 和 POD)时,您应该首先检查您的项目是否使用当前的 Swift 版本(哪个 Swift 版本是您的)您可以在 Target > Build Settings > Swift Language Version 下找到当前设置的项目)。
At this moment your project doesn't build, so make your project build by using a previous Xcode version to make your life easier and also to be sure that by upgrading everything you don't end up with broken functionality from other PODs that might have changed the way their implementation works with your project.
此时您的项目尚未构建,因此请使用以前的 Xcode 版本构建您的项目,让您的生活更轻松,并确保通过升级所有内容,您最终不会因其他 POD 的功能损坏而失败改变了他们的实施与您的项目的工作方式。
When your projects build without errors and you want to upgrade to a new version of Swift
当您的项目构建没有错误并且您想升级到新版本的 Swift 时
- Research on the latest version of your PODsand which Swift version they support (go the project github pages, or in cocoapods)
Try to upgrade just your project without upgrading your PODs, if that works you are going to upgrade also your PODs later on, to ensure your PODs stays with the current Swift version that is working for you (let's say it is right now
Swift 3.2
for example) you add this snippet to your Podfile:post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config| config.build_settings['SWIFT_VERSION'] = '3.2' end end end
To convert just your project to the newer Swift versiongo to
Edit -> Convert -> Convert to current swift syntax
A popup will appear with a list of targets, including the pods. Deselect everything except your project target, the unit and UI tests and press convert. Wait for the project to build and generate the preview and apply the changes. Fix all the issues and warning created by the new Swift version requirements.To update all the PODs that support the new Swift versionyou upgraded your project, do it by using the right POD version for each POD that has support to the newer Swift version and for the one that don't support yet the newer Swift version you could replace the code snippet on the Podfile with this
post_install do |installer| installer.pods_project.targets.each do |target| if ['UnsupportedPod1', 'RxSwift', 'RxCocoa'].include? target.name target.build_configurations.each do |config| config.build_settings['SWIFT_VERSION'] = '3.2' end end end end
- 研究您的 POD 的最新版本以及它们支持的 Swift 版本(转到项目 github 页面,或在 cocoapods 中)
尝试只升级您的项目而不升级您的 POD,如果可行,您稍后还将升级您的 POD,以确保您的 POD 与适用于您的当前 Swift 版本保持一致(例如,假设它是现在
Swift 3.2
)您将此代码段添加到您的 Podfile 中:post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config| config.build_settings['SWIFT_VERSION'] = '3.2' end end end
要将您的项目仅转换为较新的 Swift 版本,请转到
Edit -> Convert -> Convert to current swift syntax
弹出窗口,其中包含目标列表,包括 pod。取消选择除项目目标、单元和 UI 测试之外的所有内容,然后按转换。等待项目构建并生成预览并应用更改。修复由新 Swift 版本要求产生的所有问题和警告。要更新所有支持您升级项目的新 Swift 版本的 POD,请为每个支持较新 Swift 版本的 POD 和不支持较新 Swift 版本的 POD 使用正确的 POD 版本可以用这个替换 Podfile 上的代码片段
post_install do |installer| installer.pods_project.targets.each do |target| if ['UnsupportedPod1', 'RxSwift', 'RxCocoa'].include? target.name target.build_configurations.each do |config| config.build_settings['SWIFT_VERSION'] = '3.2' end end end end