52

I am getting this error on generating an NSManagedObject in Xcode 8.1 in Swift.

:0: error: filename "DemoOne+CoreDataClass.swift" used twice: '/Users/Swasidhant/Desktop/demo again/DemoOne+CoreDataClass.swift' and '/Users/Swasidhant/Library/Developer/Xcode/DerivedData/demo_again-hiinrbwwbmyfbrbctsfdzvudkkuy/Build/Intermediates/demo again.build/Debug-iphonesimulator/demo again.build/DerivedSources/CoreDataGenerated/Model/DemoOne+CoreDataClass.swift' :0: note: filenames are used to distinguish private declarations with the same name :0: error: filename "DemoOne+CoreDataProperties.swift" used twice: '/Users/Swasidhant/Desktop/demo again/DemoOne+CoreDataProperties.swift' and '/Users/Swasidhant/Library/Developer/Xcode/DerivedData/demo_again-hiinrbwwbmyfbrbctsfdzvudkkuy/Build/Intermediates/demo again.build/Debug-iphonesimulator/demo again.build/DerivedSources/CoreDataGenerated/Model/DemoOne+CoreDataProperties.swift' :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

I searched for this and got answers saying the file might be present twice in the project which is not my case. Another solution I came across was to remove the file from the build phase and add again. That too didn't work. Is it some build setting configuration mistake ? Or something else?

Here is the link with a demo project created with Xcode 8.1 and showing this problem:- https://www.dropbox.com/s/xwyzhshktb2hqe7/demo2.zip?dl=0

Swasidhant
  • 1,231
  • 1
  • 12
  • 13
  • 4
    1) Clean the project 2) Clear the Derived data 3) In build phases under compile source section (not sure about the exact wording) there might be two files, delete any one – Midhun MP Nov 01 '16 at 18:33
  • tried that earlier...tried once more...but its not working ... :( – Swasidhant Nov 01 '16 at 18:46
  • I have added a link to a sample project which shows this error. Please do have a look. – Swasidhant Nov 01 '16 at 18:55
  • I followed @MidhunMP solution (the currently accepted answer) but was having the same problems as described in the comments... I had to save the xcdatamodel file and then I was back to normal. – zabaat Dec 11 '16 at 20:45
  • I solve it by [Remove the used twice file from Compile Sources](https://stackoverflow.com/a/48339399/6521116). Refer to [Xcode/Swift 'filename used twice' build error](https://stackoverflow.com/q/34838184/6521116) – LF00 Jan 19 '18 at 10:54

8 Answers8

120

The issue is happening because of the Xcode automatic subclass generation feature.

According to What's New In Core Data

Xcode automatic subclass generation

Xcode now supports automatic generation of NSManagedObject subclasses in the modeling tool. In the entity inspector:

  • Manual/None is the default, and previous behavior; in this case you should implement your own subclass or use NSManagedObject.
  • Category/Extension generates a class extension in a file named like ClassName+CoreDataGeneratedProperties. You need to declare/implement the main class (if in Obj-C, via a header the extension can import named ClassName.h).
  • Class Definition generates subclass files named like ClassName+CoreDataClass as well as the files generated for Category/Extension.

The generated files are placed in DerivedData and rebuilt on the first build after the model is saved. They are also indexed by Xcode, so command-clicking on references and fast-opening by filename works.


So for fixing your issue, you need to set the Codegen field value to Manual/None as shown below: Core Data Codegen

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • 7
    This does not solve it for me, instead. This field is also resettled after opening the project again. – regetskcob Nov 19 '16 at 21:15
  • 2
    @DanielBocksteger: I've downloaded the linked project and did this step and it worked for me. Are you facing same error after trying the above mentioned step ? – Midhun MP Nov 20 '16 at 17:50
  • 1
    I'm also facing the same problem that @DanielBocksteger has mentioned in his comment – Vignesh Nov 28 '16 at 07:00
  • @MidhunMP Version 8.1, it worked after sometime in the linked project. But in my project, the value gets reset everytime I reopen the project – Vignesh Nov 28 '16 at 07:10
  • This does not work. I've created my model multiple times, and each time set `Codegen` to `Manual/None`. I've cleaned the build, the derived data folder through xcode, and manually 100 times. The same error, filenames used twice. – toast Dec 05 '16 at 06:17
  • 5
    To completely solve this issue, please set "Module" value (above Codegen) to "Current Product Module" – Eugene Pavlov Dec 19 '16 at 02:03
  • Setting the Module to "Current Product Module" works for me. This was tested on XCode 8.1. – superm0 Dec 29 '16 at 05:22
  • You may also have to quit Xcode and redo this a couple times. – Sean Feb 10 '17 at 23:56
  • 1
    This solved my problem AND taught me something about the latest Xcode I didn't know before. Thanks. – Jess McKenzie Aug 04 '19 at 02:22
13

This is what I did to fix this issue,

  1. Select your entity in the data model
  2. Set the Module to Current Product Module
  3. Leave the Codegen to Class Definition though Manual/None also works
  4. Select Editor > Create NSManagedObject Subclass
superm0
  • 963
  • 11
  • 19
  • If you have custom code for the model classes and opt to autogenerate the files as described in #3 then the compiler will complain about having duplicates when you build. Adding this to your answer would help save time for the reader. – Tommie C. Jan 13 '19 at 19:05
8

May help someone, Go to project compile source delete the extra reference if any.

Naveen Shan
  • 9,192
  • 3
  • 29
  • 43
5

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

It will work.

Yogendra Singh
  • 2,063
  • 25
  • 20
4

The issue was due a second file named:"Extensions.swift" in my project.

After deleting the duplicate "Extensions.swift" the error got resolved. Your error message might tell you which file is duplicated.

Darkwonder
  • 1,149
  • 1
  • 13
  • 26
0

In my case Data Model (as example: MyCoreData.xcdatamodeld) was listed twice under Editor -> Create NSManagedObject Subclass... And here was reason why it was getting this type of error.

What I did:

  1. I did delete MyCoreData.xcdatamodeld file.
  2. Created another Data Model as example MyCD.xcdatamodeld.
  3. Checked is here duplicated Data Models in here: Editor -> Create NSManagedObject Subclass...
  4. Clean and later build for two times.
Ramis
  • 13,985
  • 7
  • 81
  • 100
0

It happened to me when I copied and renamed entity. It failed to rename the class, so do it manually.

Display Name
  • 4,502
  • 2
  • 47
  • 63
0

I just restart my Macbook and that clean my build folder. And it works like a charm!

Firda Sahidi
  • 1,227
  • 1
  • 11
  • 22