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?
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With