ios Xcode/Swift“文件名使用两次”构建错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34838184/
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/Swift 'filename used twice' build error
提问by RobertyBob
I'm new to Swift and am struggling with an error after I have (possibly) correctly installed a 3rd party framework via CocoaPods.
我是 Swift 的新手,在我(可能)通过 CocoaPods 正确安装了 3rd 方框架后,我遇到了错误。
The error is as follows.
错误如下。
<unknown>:0: error: filename "MainController.swift" used twice: '/Users/myname/Desktop/ProjectName/ProjectName/Controllers/MainController.swift' and '/Users/myname/Desktop/ProjectName/ProjectName/Controllers/MainController.swift'
<unknown>:0: note: filenames are used to distinguish private declarations with the same name
Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1
As far as I can gather from other forums and seemingly obvious is that I have to remove one of the files being used twice. However I can't see where they would be installed twice. If I remove the file from the /Controllers folder the error becomes 'file not found' etc.
据我从其他论坛收集的信息,似乎很明显的是我必须删除一个被使用两次的文件。但是我看不到它们会在哪里安装两次。如果我从 /Controllers 文件夹中删除文件,错误会变成“找不到文件”等。
So my question is, where would these conflicting files be located and how do I safely remove one of them? Or is it more that there is only one file but it is being used twice - in which case how do I stop that?
所以我的问题是,这些冲突文件位于何处,如何安全地删除其中之一?或者更多的是只有一个文件但它被使用了两次 - 在这种情况下我该如何停止?
Many thanks.
非常感谢。
Ok I feel that comments are getting lost so am clarifying here to see if can trigger an answer as not been able to build and run this since yesterday.
好的,我觉得评论正在丢失,所以我在这里澄清一下,看看是否可以触发答案,因为从昨天开始就无法构建和运行它。
I now have only the swift files in the Pods->Pods/MainController folder.
我现在只有 Pods->Pods/MainController 文件夹中的 swift 文件。
When I build I get a file not found. However the file it is looking for is in App->Controllers folder which is where I deleted it from. If I put the file in there I get the 'used twice' error.
当我构建时,我找不到一个文件。然而,它正在寻找的文件位于 App->Controllers 文件夹中,我从中删除了它。如果我将文件放在那里,我会收到“使用两次”错误。
So how do I stop it trying to find the file in the Controllers and look for it in the Pods?
那么如何阻止它尝试在控制器中查找文件并在 Pod 中查找它呢?
Thanks
谢谢
采纳答案by Ankit Thakur
Try to search, if you have added same ViewController - MainController.swift
twice.
尝试搜索,如果您添加了相同的 ViewController -MainController.swift
两次。
If not, then search class MainController
in codebase.
如果没有,则class MainController
在代码库中搜索。
Regarding Pods, it is preferred to use all Pods as frameworks,
关于Pods,优先使用所有Pods作为框架,
So try using 'use_frameworks!'
in your Podfile
, and then from terminal reinstall the pods "pod install
".
所以尝试'use_frameworks!'
在你的Podfile
,然后从终端重新安装豆荚“ pod install
”。
回答by franck
Check the file is not included twice in the target's "Build Phases" "Compile Sources" settings. It can happen when you merge project.pbxproj.
检查目标的“构建阶段”“编译源”设置中没有包含两次文件。当您合并 project.pbxproj 时可能会发生这种情况。
回答by Josh O'Connor
If this is core data related and you are not having Xcode generate the entities for you at build time (you created them manually yourself), and you are experiencing this issue, chances are that you didn't check update the entity in the .xcdatamodelld file. What happens is that Xcode will create a duplicate class of your existing class, since you told Xcode to generate the entity at runtime.
In the Data Model Inspector change "Module" to "Current Product Module", and "Codegen" to "Manual/None".
如果这是与核心数据相关的,并且您没有在构建时让 Xcode 为您生成实体(您自己手动创建它们),并且您遇到此问题,则可能是您没有检查更新 .xcdatamodelld 中的实体文件。发生的情况是 Xcode 将创建现有类的重复类,因为您告诉 Xcode 在运行时生成实体。
在数据模型检查器中,将“模块”更改为“当前产品模块”,将“Codegen”更改为“手动/无”。
回答by Kris Roofe
回答by Josh
The main idea is... yes, check for duplicated classes. But in my case I had none. I had a big project, added WatchKit, lots of stuff and at some point I had a "duplicated" class. I removed the duplicate but the problem remained. To fix it I did this:
主要思想是......是的,检查重复的类。但就我而言,我没有。我有一个大项目,添加了 WatchKit,很多东西,在某个时候我有一个“重复”的类。我删除了重复项,但问题仍然存在。为了解决它,我这样做了:
- First of all, make a backup of the offending file in a folder outside your project.
- Remove the duplicate file, and the initial file
- For each of your targets: go to Build Phases, and remove ALL mentions of the problematic file.
- Add the file again to your project, and Voila!
- 首先,在项目外的文件夹中备份有问题的文件。
- 删除重复文件和初始文件
- 对于您的每个目标:转到构建阶段,并删除所有提及的有问题的文件。
- 再次将文件添加到您的项目中,瞧!
回答by Luan Henrique Damasceno Costa
This can be in your project.pbxprojwhen you make merge with githubor similar tool.
当您与github或类似工具合并时,这可以在您的project.pbxproj 中。
This can be fix opening the project.pbxprojin your favorite text editor and removing the duplicates declarations.
这可以修复在您喜欢的文本编辑器中打开project.pbxproj并删除重复声明。
Hope that fix your problem.
希望能解决您的问题。
回答by Alexander Stepanishin
To check if file is included in compile sources twice without scrolling through hundreds of flies do this:
要检查文件是否包含在编译源中两次而不滚动数百个苍蝇,请执行以下操作:
- select file in Xcode
- go to file inspector on the right panel (top left icon)
- uncheck and check again target checkbox
- 在Xcode中选择文件
- 转到右侧面板上的文件检查器(左上角图标)
- 取消选中并再次选中目标复选框
回答by Craig1481
I would add on to Josh O'Conners answer in that making the changes he recommended was the root of my problem. I did have to clean the build folder by hitting shift-option-command-k to complete the fix.
我会补充 Josh O'Conners 的回答,因为他推荐的更改是我问题的根源。我确实必须通过点击 shift-option-command-k 来清理构建文件夹才能完成修复。
回答by Yogendra Singh
In the case of code data model classes. Select code data model then select the Entity change the value of 'Codegen' to Manual/None.
在代码数据模型类的情况下。选择代码数据模型,然后选择实体,将“Codegen”的值更改为手动/无。
It helps for me.
它对我有帮助。
回答by Honey
I just ran into this issue myself. It was the first time that I was adding a new target to a project.
我自己刚刚遇到了这个问题。这是我第一次向项目添加新目标。
When you're adding a new target you should click on an existing target and attempt to DUPLICATE it.
添加新目标时,您应该单击现有目标并尝试复制它。
You shouldn't create the target from the same template. As that would add a new AppDelegate, ViewController, SceneDelegate, plist, Main.Storyboard and Assets. It would basically add everything that template gives and your project now has 2 of each of those fails. If you then go and add the target to the existingAppDelegate then you'll get the error mentioned. The fix is to:
您不应从同一模板创建目标。因为这会添加一个新的 AppDelegate、ViewController、SceneDelegate、plist、Main.Storyboard 和 Assets。它基本上会添加模板提供的所有内容,并且您的项目现在有 2 个失败。如果您随后将目标添加到现有的AppDelegate,那么您将收到提到的错误。解决方法是:
- Delete all duplicate files
- As Franck said: Check the file is not included twice in the target's "Build Phases" "Compile Sources" settings. It can happen when you merge project.pbxproj.
- 删除所有重复文件
- 正如弗兰克所说:在目标的“构建阶段”“编译源”设置中检查文件是否包含两次。当您合并 project.pbxproj 时可能会发生这种情况。
If you duplicate, then all you get is a new target and a plist. Once you get those then for every existing file you need to go in its file inspector and add the new target to the list of its targets.
如果你复制,那么你得到的只是一个新的目标和一个 plist。一旦你获得了这些,那么对于每个现有文件,你需要进入其文件检查器并将新目标添加到其目标列表中。