spin_eight spin_eight - 1 year ago 1970
Swift Question

Xcode-beta 8. Can't create core data

I have been trying for 2 days already 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:

import Foundation
import CoreData


EntityName +CoreDataClass.swift:

import Foundation
import CoreData

class EntityName: NSManagedObject {



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?

Answer Source

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.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download