xcode 在使用 cocoapods 的 swift 2.3 项目中使用 Xcode8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38618617/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:05:13  来源:igfitidea点击:

Using Xcode8 with a swift 2.3 project that uses cocoapods

swiftxcodecocoapodsswift3

提问by Alex Bollbach

I have a project with a light amount of code written in what I assume is swift 2.3 It contains and app extension also written in swift 2.3 and uses 2 Cocoapods: SwiftyJSONand MMWormhole. After downloading Xcode 8.3 beta, the migrator ran and I am left with almost 100 compiler errors in the one main swift file contained in SwiftyJSON.

我有一个项目,其中包含用 swift 2.3 编写的少量代码,它包含和应用程序扩展也是用 swift 2.3 编写的,并使用 2 个 Cocoapods:SwiftyJSONMMWormhole. 下载 Xcode 8.3 beta 后,迁移器开始运行,我在SwiftyJSON.

Basically I want to know if there is a way I can work in Xcode8 given these details. I am happy to update my own code to swift3 however I do not control the cocoapods (MMWormHole is in objective-C so I assume that Xcode converts that to whichever version of Swift it needs as it emits no compiler errors). Can I tell Xcode to use swift 2.3 globally?

基本上我想知道是否有一种方法可以根据这些细节在 Xcode8 中工作。我很高兴将我自己的代码更新为 swift3,但是我不控制 cocoapods(MMWormHole 在目标 C 中,所以我假设 Xcode 将其转换为它需要的任何版本的 Swift,因为它不会发出编译器错误)。我可以告诉 Xcode 在全球范围内使用 swift 2.3 吗?

回答by Buntylm

You have to set Use Legacy Swift Language Versionto YESto make use SWIFT 2.3code in Xcode 8. Then add this into your Podfileto make all of your pod targets confirm the same.

你必须设置Use Legacy Swift Language VersionYES利用SWIFT 2.3代码Xcode 8。然后将其添加到您的Podfile所有 pod 目标中以确认相同。

post_install do |installer| 
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |configuration|
      configuration.build_settings['SWIFT_VERSION'] = "2.3" 
    end 
  end 
end

I hope, it will help.

我希望,它会有所帮助。

回答by Deyton

Many open source Swift projects have branches for Swift 3 or Swift 2.3 (see this postfor details on a popular approach). I checked SwiftyJSON and it appears to have a branch for Swift 3, so you could convert your app to Swift 3 and give that a try. In order to use it, change the SwiftyJSON entry in your Podfile to:

许多开源 Swift 项目都有 Swift 3 或 Swift 2.3 的分支(有关流行方法的详细信息,请参阅此帖子)。我检查了 SwiftyJSON,它似乎有一个适用于 Swift 3 的分支,因此您可以将您的应用程序转换为 Swift 3 并尝试一下。为了使用它,请将 Podfile 中的 SwiftyJSON 条目更改为:

pod 'SwiftyJSON', :git => 'https://github.com/SwiftyJSON/SwiftyJSON.git', :branch => 'swift3'

It's up to the project to update for each Xcode 8 beta, so it may not be exactly working, but it is likely that there will be fewer than 100 errors.

每个 Xcode 8 测试版的更新取决于项目,因此它可能无法正常工作,但可能会出现少于 100 个错误。

Note: You may see a “Use Legacy Swift Language Version” error after updating everything and fixing the complier errors. This can be fixed by adding a post_installstep to your Podfile (see this GitHub issue), or by updating to CocoaPods 1.1.0.beta.1 or higher (gem install cocoapods --pre).

注意:在更新所有内容并修复编译器错误后,您可能会看到“使用旧版 Swift 语言版本”错误。这可以通过向post_installPodfile添加一个步骤(请参阅此 GitHub 问题)或更新到 CocoaPods 1.1.0.beta.1 或更高版本 ( gem install cocoapods --pre) 来解决。

回答by Cosmic Arrows

From my experience when starting up the workspace, the SDK should ask you if you'd like to convert your code to Swift 3 or do it "later". By just selecting later, it won't migrate your code to swift 3. I must warn you though that I went through the same thing and it was almost impossible to work backwards just because you want to use the latest and greatest Xcode 8. You'll eventually run into problems such as when you're ready to push to the app store and iTunesConnect won't accept any files that are lower than version 10. Also when and if another developer inherits your code, they will have problems if they are using an earlier version of Xcode.

根据我在启动工作区时的经验,SDK 应该询问您是否要将代码转换为 Swift 3 或“稍后”执行。通过稍后选择,它不会将您的代码迁移到 swift 3。我必须警告您,尽管我经历了同样的事情,并且几乎不可能仅仅因为您想使用最新最好的 Xcode 8 而向后工作。最终会遇到一些问题,例如当您准备好推送到应用程序商店时,iTunesConnect 将不接受任何低于 10 版本的文件。此外,当其他开发人员继承您的代码时,如果他们继承了您的代码,他们也会遇到问题正在使用早期版本的 Xcode。

回答by nodebase

Add the following to the end of your Podfilethen run pod install:

将以下内容添加到Podfilethen run的末尾pod install

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['SWIFT_VERSION'] = '2.3'
    end
  end
end