40

I have been trying to add core data. And every time I got the same error:

error: filename "EntityName +CoreDataClass.swift" used twice: '/Users/userName/Desktop/Development/MyApp/AppName/EntityName +CoreDataClass.swift' and '/Users/userName/Library/Developer/Xcode/DerivedData/AppName-dgwzrmxsetzvtedibxrazuutjwnh/Build/Intermediates/AppName.build/Debug-iphoneos/AppName.build/DerivedSources/CoreDataGenerated/Model/EntityName +CoreDataClass.swift'

I add core data using the following steps:
1.New file/ DataModel; save it in the root dir of my project
select Model.xcdatamodeld and add entity, add several attributes, save, editor/create NSManagedObjectClass Subclass.

As a result I observe 4 new files in navigator: Model.xcdatamodeld, EntityName+CoreDataProperties.swift, EntityName +CoreDataClass.swift, _COREDATA_DATAMODELNAME_+CoreDataModel.swift

their content: _COREDATA_DATAMODELNAME_+CoreDataModel.swift:

import Foundation
import CoreData

___COREDATA_DATAMODEL_MANAGEDOBJECTCLASSES_IMPLEMENTATIONS___

EntityName +CoreDataClass.swift:

import Foundation
import CoreData


class EntityName: NSManagedObject {

}

EntityName+CoreDataProperties.swift:

import Foundation
import CoreData

extension EntityName {

    @nonobjc class func fetchRequest() -> NSFetchRequest< EntityName > {
        return NSFetchRequest< EntityName >(entityName: "EntityName");
    }

    @NSManaged var str: String?

}

What I have tried:
1. Clean build, remove DerivedData, delete content of var/folders, restart
2. Delete generated files, displayed in navigator

All my efforts were out of luck.
What I am doing wrong?

spin_eight
  • 3,925
  • 10
  • 39
  • 61

9 Answers9

53

There are two bugs in XCode 8 here:

1 - If you change the Codegen dropdown, it's new value isn't saved in Model.xcdatamodel. You have to change something else to get it to save. For example change the class name; build; change the class name back; build again.

2 - The generated code is placed in DerivedData in the Intermediates folder, but it only happens if the folder doesn't already exist. The workaround is to do a clean then a build.

DavidA
  • 3,112
  • 24
  • 35
  • 6
    This needs to be marked as the correct answer. Changing the name, building, changing it back, rebuilding fixed my errors. – Mike Oct 20 '16 at 21:34
  • 4
    You are a life saver! Who would have suspected that the Codegen dropdown settings isn't saved? – Rick Nov 01 '16 at 04:32
  • You just saved me! This trick about change something in the model classes to save code-gen setting - just awesome! Thanks! – iiFreeman Nov 13 '16 at 23:53
  • Just to be clear on one thing, the option you should select is `Manual/None`, right? I know in previous versions it generated two files, I assume that this is for the purposes of subclassing and making changes to the non-generated code like adding custom code??? – Hatem Jaber Dec 21 '16 at 21:06
45

Xcode 8 includes automatic NSManagedObject class generation when the model file uses the Xcode 8 file format. If you create your own subclass files, you're creating duplicates. The second file in the error message, in DerivedSources, is the one that Xcode created automatically.

If the automatically generated files do what you need, just stop creating your own and you'll be OK.

If you want to create your own subclasses instead, you can either

  • Set the "tools version" for the model file to be Xcode 7.3 or earlier to disable all code generation (this doesn't seem to change anything meaningful about the actual file contents), or
  • Disable automatic generation for each entity individually by setting the "Codegen" setting to "Manual/None" for the entity.
Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • Hello! I shouldn't create a custom NSManagedObject subclass? I don't understand how to use it on iOS 10 and Swift 3. – Amateur User Jul 20 '16 at 00:58
  • How to use a Core Data on iOS 10 and Swift 3 ? – Amateur User Jul 20 '16 at 00:59
  • Like I said I'm the answer, you can create your own subclasses if you want, just make sure that you're not creating duplicates. – Tom Harrington Jul 20 '16 at 04:33
  • I have disabled automatic subclass generation and I tried to create a custom `NSManagedObject` subclass. But it's an error like so ` _COREDATA_DATAMODELNAME_+CoreDataModel.swift ` and this file comes with a couple of error messages. – Amateur User Jul 21 '16 at 02:24
  • If you need help, ask another question. Hashing this out in the comments on some other question makes no sense. – Tom Harrington Jul 21 '16 at 03:49
  • Thank you, this saved a lot of fruitless effort. I followed the create your own... approach, including deleting the files it generated which were crashing since their names didn't start with a period (and they failed even if renamed). I did get one error in the ___COREDATA_DATAMODELNAME___+CoreDataModel.swift file that Xcode generated stating that one line was illegal, but when that line was commented out it finished the compile / install and worked fine. Has anyone filed this bug? – Ron Diel Aug 04 '16 at 17:23
12

I'm actually having the same problem (using Swift) and suspect it is a bug. If I understand correctly the OP is using Xcode's autogen NSManagedObject subclasses and is not then subsequently creating additional (superfluous) subclasses which appears to be a source of some confusion.

Steps to reproduce:

  1. Create a new project, single view application. Tick "Use Core Data'

  2. Create an entity in the model entity, add properties, ensure file is saved (sometimes Xcode 8 Beta throws the data if not explicitly saved)

  3. Select Editor -> Created NSManagedObject subclass. Tick relevant boxes

Xcode creates 3 files:

  1. COREDATA_DATAMODELNAME_+CoreDataModel.swift. This file is corrupted and contains non-valid entries following statements to import Foundation and CoreData - the project will not compile unless this file is deleted

    import Foundation
    
    import CoreData
    
    ___COREDATA_DATAMODEL_MANAGEDOBJECTCLASSES_IMPLEMENTATIONS___
    
  2. EntityName+CoreDataClass.swift

  3. EntityName+CoreDataProperties.swift

Although the editor doesn't flag any errors at this point, attempts to compile fail for the reasons listed by the OP (namely missing files with the same names but with a '.' prefix in the DerivedData folder)

If you create the NSManagedObject subclasses manually after creating your model without using Xcode's evidently bugged auto-gen, there are no issues. A bit more typing but a lot more reliable! However, you will need to start from a 'clean' project (i.e. before you attempted to auto-generate the subclasses) otherwise the error persists. Cleaning out Derived Data won't help sadly.

***** UPDATED ***** There does appear to be something rather odd going on and there does appear to be silent code generation as originally suggested (apologies) but this is very different behaviour from what one would expect. Furthermore, this code is not visible in the editor (which seems a little pointless as well as confusing). It is created in a subfolder of DerivedData > Build > Intermediates > .Build.

I can completely see why the OP was confused (as was I!) For what it's worth this 'feature' is undoubtedly an attempt to be helpful but is somewhat confusing if you're used to previous behaviour and you are offered an option to generate a visible and editable duplicate from the main menu.

So, select 'Manual/None' in the Codegen window (shown below) and then you can either use the auto-gen option in the menu bar (after deleting the 'junk') or write your own code.

New XCode 8 codegen option

rustproofFish
  • 931
  • 10
  • 32
5

Follow these steps,

  1. Disable automatic generation for each entity individually by setting the "Codegen" setting to "Manual/None" for the entity.

  2. Change entity name to something else to get it to save. For example change the class name; build; change the class name back; build again.

Baig
  • 4,737
  • 1
  • 32
  • 47
2

I got this error to,

just the change the tool version to Xcode 7.3 in your file .xcdatamodeld

and it works fine!

0

For everyone having trouble getting autogen to work:

I had to set the "com.apple.syncservices.Syncable" to "NO" on the User Info settings for the Entity.

Maybe that helps out.

Rion
  • 1
  • What do you mean by " getting autogen to work"? Do you mean setting setting Codegen to "Class Definition" or "Category/Extension"? – Elise van Looij Sep 27 '16 at 12:07
0

Tom Harrington Answer is correct. However, there is a way to add your own functions and/or vars without doing any of the two mentioned options.

Just create an extension to the class. (make sure you name the swift file something different than the standard auto-generated NSManagesObject files.)

For example. If you have an entity called MyEntity you could add a swift file called MyEntityExtension.swift which could look something like this:

import Foundation
import CoreData
import UIKit


extension MyEntity {

    var color: UIColor {
        get {
            return self.colorValue as! UIColor
        }
        set {
            if newValue.isMember(of: UIColor.self) {
                self.colorValue = newValue
            }
        }
    }

}
guido
  • 2,792
  • 1
  • 21
  • 40
0

After trying a handful of posted answers, rebooting my machine worked for me.

FelixSFD
  • 6,052
  • 10
  • 43
  • 117
AlexMath
  • 567
  • 1
  • 6
  • 16
0

Delete compile file core data in the tab "Build phases" section "Compile source" pressed -

Sasha Tsvigun
  • 311
  • 5
  • 15