ios Xcode 9 Swift 语言版本 (SWIFT_VERSION)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46338588/
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
Xcode 9 Swift Language Version (SWIFT_VERSION)
提问by Chaudhry Talha
I've recently updated xcode to version 9. Before that in Xcode 8.x whenever I use to do pod update
it shows me an update code to convert the code to Swift 3 and doing that solve the errors. But now it is giving me this error:
我最近将 xcode 更新到版本 9。在此之前,在 Xcode 8.x 中,每当我使用pod update
它时,它都会向我显示更新代码以将代码转换为 Swift 3 并解决错误。但现在它给了我这个错误:
The “Swift Language Version” (SWIFT_VERSION) build setting must be set to a supported value for targets which use Swift. This setting can be set in the build settings editor.
对于使用 Swift 的目标,“Swift 语言版本”(SWIFT_VERSION) 构建设置必须设置为支持的值。可以在构建设置编辑器中设置此设置。
For this error I've tried Build Settings > Swift Language Version
and it was Swift 3.2
But as the update call has re-installed some of the pods like Alamofire and it says to install Xcode 8.x to convert the code to Swift 3 so do I need to install an additional version with the latest Xcode? If yes, then how can I install Xcode 8 with Xcode 9?
对于这个错误,我已经尝试过Build Settings > Swift Language Version
,它是 Swift 3.2 但是随着更新调用重新安装了一些像 Alamofire 这样的 pod,它说安装 Xcode 8.x 以将代码转换为 Swift 3 所以我需要安装一个最新 Xcode 的附加版本?如果是,那么如何使用 Xcode 9 安装 Xcode 8?
回答by Krunal
Answer to your question:
You can download Xcode 8.x from Apple Download Portalor Download Xcode 8.3.3(or see: Where to download older version of Xcode), if you've premium developer account (apple id). You can install & work with both Xcode 9 and Xcode 8.x in single (mac) system. (Make sure you've Command Line Tools
supporting both version of Xcode, to work with terminal (see: How to install 'Command Line Tool'))
回答您的问题:如果您拥有高级开发者帐户 (apple id),
您可以从Apple 下载门户下载 Xcode 8.x或下载 Xcode 8.3.3(或参阅:从哪里下载旧版本的 Xcode)。您可以在单个 (mac) 系统中安装和使用 Xcode 9 和 Xcode 8.x。(确保您Command Line Tools
支持两个版本的 Xcode,以使用终端(请参阅:如何安装“命令行工具”))
Hint: How to migrate your code Xcode 9 compatible Swift versions (Swift 3.2 or 4)
Xcode 9 allows conversion/migration from Swift 3.0
to Swift 3.2/4.0
only. So if current version of Swift language of your project is below 3.0 then you must migrate your code in Swift 3 compatible version Using Xcode 8.x.
提示:如何迁移代码 兼容 Xcode 9 的 Swift 版本(Swift 3.2 或 4)
Xcode 9 只允许从Swift 3.0
到 的转换/迁移Swift 3.2/4.0
。因此,如果您项目的当前 Swift 语言版本低于 3.0,那么您必须使用 Xcode 8.x 在 Swift 3 兼容版本中迁移您的代码。
This is common error message that Xcode 9 shows if it identifies Swift language below 3.0, during migration.
这是 Xcode 9 在迁移过程中识别 Swift 语言低于 3.0 时显示的常见错误消息。
Swift 3.2 is supported by Xcode 9 & Xcode 8 both.
Xcode 9 和 Xcode 8 都支持 Swift 3.2。
Project ? (Select Your Project Target) ? Build Settings ? (Type 'swift' in Searchbar) Swift Compiler Language ? Swift Language Version ? Click on Language list to open it.
项目 ?(选择您的项目目标) ? 构建设置?(在搜索栏中输入“swift”)Swift 编译器语言?Swift 语言版本?单击语言列表将其打开。
Convert your source code from Swift 2.0 to 3.2 using Xcode 8 and then continue with Xcode 9 (Swift 3.2 or 4).
使用 Xcode 8 将源代码从 Swift 2.0 转换为 3.2,然后继续使用 Xcode 9(Swift 3.2 或 4)。
For easier migration of your code, follow these steps: (it will help you to convert into latest version of swift supported by your Xcode Tool)
为了更轻松地迁移代码,请按照以下步骤操作:(它将帮助您转换为 Xcode 工具支持的最新版本的 swift)
Xcode: Menus: Edit ? Covert ? To Current Swift Syntax
Xcode:菜单:编辑?隐蔽的?当前 Swift 语法
回答by R. Mohan
For Objective C Projects created using Xcode 8 and now opening in Xcode 9, it is showing the same error as mentioned in the question.
对于使用 Xcode 8 创建并现在在 Xcode 9 中打开的 Objective C 项目,它显示了与问题中提到的相同的错误。
To fix that, Press the + button in Build Settings and select Add User-Defined Setting as shown in the image below
要解决此问题,请按 Build Settings 中的 + 按钮,然后选择 Add User-Defined Setting,如下图所示
Then in the new row created add SWIFT_VERSION
as key and 3.2
as value like below.
然后在创建的新行中添加SWIFT_VERSION
为键和3.2
值,如下所示。
It will fix the error for objective c projects.
它将修复目标 c 项目的错误。
回答by nevan king
I just got this after creating a new Objective-C project in Xcode 10, after I added a Core Data model file to the project.
在 Xcode 10 中创建了一个新的 Objective-C 项目后,我刚刚在项目中添加了一个 Core Data 模型文件后得到了这个。
I found two ways to fix this:
我找到了两种方法来解决这个问题:
- The Easy Way: Open the Core Data model's File Inspector (??-1) and change the language from Swift to Objective-C
- 简单方法:打开 Core Data 模型的文件检查器 ( ??- 1) 并将语言从 Swift 更改为 Objective-C
- Longer and more dangerous method
- 更长更危险的方法
The model contains a "contents" file with this line:
该模型包含一个带有以下行的“内容”文件:
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="14460.32" systemVersion="17G5019" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
In there is a sourceLanguage="Swift"
entry. Change it to sourceLanguage="Objective-C"
and the error goes away.
里面有一个sourceLanguage="Swift"
入口。将其更改为sourceLanguage="Objective-C"
,错误就会消失。
To find the "contents" file, right click on the .xcdatamodeld
in Xcode and do "Show in Finder". Right-click on the actual (Finder) file and do "Show Package Contents"
要查找“内容”文件,请右键单击.xcdatamodeld
Xcode 中的“在 Finder 中显示”。右键单击实际的(Finder)文件并执行“显示包内容”
Also: Changing the model's language will stop Xcode from generating managed object subclass files in Swift.
另外:更改模型的语言将阻止 Xcode 在 Swift 中生成托管对象子类文件。
回答by digitaldaemon
This can happen when you added Core Data to an existing project.
Check the:
<Name>/<Name>.xcdatamodeld/<Name>.xcdatamodel/contents
file.
This file contains an entry "sourceLanguage" that (by default) might have been set to "Swift". Change it to "Objective-C".
当您将 Core Data 添加到现有项目时,可能会发生这种情况。
检查:
<Name>/<Name>.xcdatamodeld/<Name>.xcdatamodel/contents
文件。
该文件包含一个条目“sourceLanguage”(默认情况下)可能已设置为“Swift”。将其更改为“Objective-C”。
回答by Shubham Mishra
This Solution works when nothing else works:
当其他方法无效时,此解决方案有效:
I spent more than a week to convert the whole project and came to a solution below:
我花了一个多星期来转换整个项目,并得出以下解决方案:
First, de-integrate the cocopods dependency from the project and then start converting the project to the latest swift version.
首先,从项目中取消集成 cocopods 依赖项,然后开始将项目转换为最新的 swift 版本。
Go to Project Directory in the Terminal and Type:
转到终端中的项目目录并键入:
pod deintegrate
This will de-integrate cocopods from the project and No traces of CocoaPods will be left in the project. But at the same time, it won't delete the xcworkspace and podfiles. It's ok if they are present.
这将从项目中分离 cocopods,并且不会在项目中留下 CocoaPods 的痕迹。但同时,它不会删除 xcworkspace 和 podfiles。如果他们在场就好了。
Now you have to open xcodeproj(not xcworkspace) and you will get lots of errors because you have called cocoapods dependency methods in your main projects.
现在你必须打开 xcodeproj(不是 xcworkspace),你会得到很多错误,因为你在你的主项目中调用了 cocoapods 依赖方法。
So to remove those errors you have two options:
因此,要消除这些错误,您有两个选择:
- Comment down all the code you have used from cocoapods library.
- Create a wrapper class which has dummy methods similar to cocopods library, and then call it.
- 注释掉你在 cocoapods 库中使用过的所有代码。
- 创建一个包含类似于 cocopods 库的虚拟方法的包装类,然后调用它。
Once all the errors get removed you can convert the code to the latest swift version.
删除所有错误后,您可以将代码转换为最新的 swift 版本。
Sometimes if you are getting weird errors then try cleaning derived data and try again.
有时,如果您遇到奇怪的错误,请尝试清理派生数据并重试。
回答by dalmate
maybe you need to download toolchain. This error occurs when you don't have right version of swift compiler.
也许您需要下载工具链。当您没有正确版本的 swift 编译器时会发生此错误。
回答by Atal Singh
I just click on latest swift convert button and set App target build setting-> Swift language version: swift 4.0,
我只需单击最新的 swift 转换按钮并设置 App 目标构建设置-> Swift 语言版本:swift 4.0,
Hope this will help.
希望这会有所帮助。
回答by iman kazemayni
In my case, all warn disappeared after I directly changed swift version from 2.x to 4.0 in build settings except two warn.
就我而言,除了两个警告之外,在构建设置中直接将 swift 版本从 2.x 更改为 4.0 后,所有警告都消失了。
These warning related to myprojectnameTests
and myprojectnameUITests
folder. I didn't notice and I thought its relate to direct immigration from Xcode 7 to Xcode 9 and I thought I couldn't solve this problem and I should install missed Xcode 8 version.
这些警告涉及到myprojectnameTests
和myprojectnameUITests
文件夹。我没有注意到,我认为它与从 Xcode 7 到 Xcode 9 的直接迁移有关,我认为我无法解决这个问题,我应该安装错过的 Xcode 8 版本。
In my case, I deleted these folders and all warns disappeared, but you can recreate this folder and contains using this:
就我而言,我删除了这些文件夹,所有警告都消失了,但您可以使用以下命令重新创建此文件夹并包含:
file > new > target > (uitest or unittest extensions)
file > new > target > (uitest or unittest extensions)
and use this article for create test cases: https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/04-writing_tests.html
并使用本文创建测试用例:https: //developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/04-writing_tests.html