50

I'm new to Swift and am struggling with an error after I have (possibly) correctly installed a 3rd party framework via CocoaPods.

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.

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.

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.

So how do I stop it trying to find the file in the Controllers and look for it in the Pods?

Thanks

LF00
  • 27,015
  • 29
  • 156
  • 295
RobertyBob
  • 803
  • 1
  • 8
  • 20

11 Answers11

163

Check the file is not included twice in the target's "Build Phases" "Compile Sources" settings. It can happen when you merge project.pbxproj.

franck
  • 2,995
  • 3
  • 17
  • 28
87

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".
enter image description here

Josh O'Connor
  • 4,694
  • 7
  • 54
  • 98
  • I just had to change the name in the Name field. I don't know why there were two Entities with same names here. And then had to do a full clean and rebuild. – zeeshan Mar 30 '18 at 13:59
  • You save my day! But things strange is that, I am using XCode 10.1 Create MangedObject Subclass, but the Module and Codegen didn't setup for me.... Maybe it's XCode's bug. – Orange Nov 03 '18 at 22:29
  • I had to 'Product > Clean Build Folder' after I changed the Codegen to Manual/None and the error disappeared. – stevo.mit Mar 08 '19 at 13:30
16

I came across this issue by remove the used twice file from Compile Sources as below image. Selected the file, and click the -. Then clean and build.

enter image description here

LF00
  • 27,015
  • 29
  • 156
  • 295
13

Try to search, if you have added same ViewController - MainController.swift twice.

If not, then search class MainController in codebase.

Regarding Pods, it is preferred to use all Pods as frameworks,

So try using 'use_frameworks!' in your Podfile, and then from terminal reinstall the pods "pod install".

Ankit Thakur
  • 4,739
  • 1
  • 19
  • 35
  • Many thanks Ankit. By using the search for classes I have found 2 instances - one in Pods and one in the Project. Since the one in the project is the code for the ViewController, can I assume I delete the one in the Pods (the whole MainController folder or just the swift file within)? – RobertyBob Jan 17 '16 at 13:43
  • 1
    To clarify - I have duplicates of the Swift files (and therefore the classes). One is within the main App in /Controllers folder alongside all other Swift files. The other is with a Pods App->Pods folder. The Pods App seems to contain the other 3rd party folders. If I delete the Swift file in the main app i get file not found error but have a feeling that this is the one needing removed. So do I need to point something at the Pods folder instead? – RobertyBob Jan 18 '16 at 13:09
  • can you refer, which pod file you are using. Actually file name either in Pods or in local bundle should not have same name, because during compile time, it will give error due to class reference. Here is one of the issue, which other developers have also found: https://github.com/CocoaPods/CocoaPods/issues/4166 – Ankit Thakur Jan 18 '16 at 16:21
  • There is no longer an issue with duplicate classes since I removed the swift files from the main app files. The only location for the swift file is in Pods>Pods>PagingMenuController however the build keeps wanting to find it in the main app files and can't since I removed it. I've tried adding all sorts to the bridging header file but nothing stops it wanting to find the file in the /controllers folder of the main app – RobertyBob Jan 18 '16 at 16:35
  • 3
    ok this is my issue with this - I randomly found a reference to the /controllers/pagingmenucontroller file in the Build Phases>Compile Sources. This seemed to be a likely culprit so I removed it since the file doesn't exist..... Now my bridging header fails cos RestKit.h can't be found even tho this has never ever been an issue before. – RobertyBob Jan 18 '16 at 17:45
  • so i just kept cleaning, building, pod installing until errors eliminated themselves (pod is riddled with errors so went through this for nothing but learnt a bit on the way) – RobertyBob Jan 19 '16 at 12:12
  • actually this isn't fixed as RestKit.h still isn't found no matter what I do but figure this is a different issue so will see if allowed to add it as a new question – RobertyBob Jan 19 '16 at 14:34
  • 1
    In my case Double entry in Project Settings>"Build Phrases" was the issue. So @josh s answer is the correct answer in my case. – Nikhil Mathew Jul 11 '16 at 23:04
11

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:

  • 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!
Josh
  • 6,251
  • 2
  • 46
  • 73
8

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

enter image description here

6

This can be in your project.pbxproj when you make merge with github or similar tool.

This can be fix opening the project.pbxproj in your favorite text editor and removing the duplicates declarations.

Hope that fix your problem.

pringi
  • 3,987
  • 5
  • 35
  • 45
  • Yes that solved the problem. There were 2 lines next to each other that declared same file name after merge in the project.pbxproj. Thank you! – SchmidtFx Apr 01 '20 at 06:33
3

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.

Craig1481
  • 39
  • 4
0

In the case of code data model classes. Select code data model then select the Entity change the value of 'Codegen' to Manual/None.

It helps for me.

Yogendra Singh
  • 2,063
  • 25
  • 20
0

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 existing AppDelegate then you'll get the error mentioned. The fix is to:

  1. Delete all duplicate files
  2. 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.

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.

mfaani
  • 33,269
  • 19
  • 164
  • 293
-1

I resolved this twice filename issue with change of Project settings

Here i have attached for project setting changes.

Legacy build system.

Hope it will help !!

Pankaj bhalala
  • 163
  • 1
  • 11