Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode-beta 8. Can't create core data

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?

like image 912
spin_eight Avatar asked Jul 16 '16 07:07

spin_eight


People also ask

How do I enable Core Data in Xcode?

Get our help adding Core Data to your project So, with your existing project open, create a new project in Xcode (⇧⌘N) and select a Single View App, you can call it whatever you like as we'll be deleting it when we're done. You'll see the “Use Core Data” checkbox on the project options screen, make sure it is checked.

How do I set up NSManagedObject?

From the Xcode menu bar, choose Editor > Create NSManagedObject Subclass. Select your data model, then the appropriate entity, and choose where to save the files. Xcode places both class and properties files into your project.


2 Answers

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.

like image 76
DavidA Avatar answered Oct 06 '22 00:10

DavidA


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.
like image 28
Tom Harrington Avatar answered Oct 06 '22 01:10

Tom Harrington