NSManagedObject subclasses duplicate declaration
You are generating files which have already been generated for you by Xcode and thus get duplicate declarations.
Find detailes in this answer.
Duplicate Symbol Error in NSManagedObject Subclass
You are generating files which have already been generated for you by Xcode and thus get duplicate declarations. Details about this feature (new in Xcode 8) can be found in this WWDC video.
Two possible fixes:
1) Use the Xcode generated ManagedObject
subclasses (the recommended, modern approach)
- Delete all generated NSManagedObject subclasses from your project, if exists.
- Set
Codegen
toClass Definition
in your.xcdatamodel
for all entities - Make sure
Module
is empty ("Global Namespace" in light gray) (workaround an Apple bug, see this answer)
- Clean project
- Clean DerivedData folder (Optional. To be on the save side)
- build
Note:
Never add the automatically generated files to your project. Even you do not see the generated files in your project, Xcode has a reference to it, so you are able to write extensions and such. For instance:
extension MyEntity {
func doSomething() {
//
}
}
Also, you can command+click to the generated file within Xcode.
2) Trigger subclass generation manually (a rather paranoid but bullet-prove approach, ignoring the new Xcode features)
- Delete all generated NSManagedObject subclasses from your project, if exists.
- Set
Codegen
toManual/None
in your.xcdatamodel
for all entities - Clean project
- Clean DerivedData folder
- Restart Xcode
- Manually generate
NSManagedObject
subclasses (in "Editor" menu) - Make sure those files are added to your project
- build
Xcode 8 generates broken NSManagedObject subclasses for iOS 10
I finally got mine to work. Here is what I did. (Flights is one of my entities)
I setup the xcdatamodeld as follows
And then the entity as
Then I used Editor -> Create NSManagedObject Subclass
This creates two files for my flights entity
Flights+CoreDataProperties.swift
Flights+CoreDataClass.swift
I renamed Flights+CoreDataClass.swift to Flights.swift
Flights.swift is just
import Foundation
import CoreData
@objc(Flights)
public class Flights: NSManagedObject {
}
Flights+CoreDataProperties.swift is
import Foundation
import CoreData
extension Flights {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Flights> {
return NSFetchRequest<Flights>(entityName: "Flights");
}
@NSManaged public var ...
}
This appears to work for me.I could not get Codegen to work in any other way, even though I tried many of the suggestions that were out there.
Also this had me scratching my head for a while and I add it as an assist. Don't forget with the new Generics version of the FetchRequest you can do this
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "Flights")
Error when creating NSManaged Subclass in Xcode 8.2 beta
The issue here is that as of Xcode 8, new projects using Core Data and new Core Data data models are configured to use automatic code generation by default. That means you don’t have to explicitly generate code for your Core Data entities any more, Xcode will take care of that for you. If you leave automatic code generation on and also manaully generate Swift classes for your entities, you’ll see an error like this.
You can disable automatic code generation for an entity in the entity inspector, or you can remove the manually-generated code from your project. Either of the two should fix this.
Errors after 'Create NSManagedObject Subclass for CoreData Entities
The current default in Xcode is to automatically create subclasses of NSManagedObject for you in the /Users/<your user name>/Library/Developer/Xcode/DerivedData/AppName-agkwmibzbopevjgfajlcbyixzyev/Build/Intermediates/AppName.build/Debug-iphonesimulator/AppName.build/DerivedSources/CoreDataGenerated/Model
directory; The DerivedData directory is where Xcode saves automatically generated code. You are redeclaring the same subclass by doing Editor>Create NSManagedObject Subclass... that is why you are getting the "Invalid redeclaration of 'UserRoutine' 'UserExercise' is ambiguous for type lookup in this context @NSManaged only allowed on an instance property or method"
error. To resolve the errors and manually create a subclass of NSManagedObjects what you need to do is:
Open terminal and navigate to
/Users/<your user name>/Library/Developer/Xcode/DerivedData/AppName-agkwmibzbopevjgfajlcbyixzyev/Build/Intermediates/AppName.build/Debug-iphonesimulator/AppName.build/DerivedSources/CoreDataGenerated/Model
Run this command:
rm -rf *
(now be careful with this command, run it only when you get to the final directory where the generated codes are or you'll break your project for good)Delete your current data model
Create a new data model
Select your new data model's entity (do this for each entity within the data model) and go to its attributes inspector and set its Codegen to
Manual/None
Before the first run after you have created a new data model.Create a subclass of NSManagedObject by going to Editor>Create NSManagedObject Subclass...
Your errors should disappear.
Hope this helped!
Create NSManagedObject Subclass... make a new Error in my project
Select the xcdatamodeld
file in the project navigator, select the entity and press ⌥⌘3 – in Xcode 13 ⌥⌘4 – to show the Data Model Inspector.
If you created the class manually the Codegen
popup must be set to Manual/None
otherwise the class file is created implicitly.
Updating NSManagedObject subclass after updating data model
Here is what I do for this case. You have to do it only once, with no more work required for future changes to the data model.
I create separate .h
and .m
files for my NSModeledObject
subclasses with a category. For an entity called, say, "Event", it would look like Event+Helper.h
and start like this:
#import "Event.h"
@interface Event (Helper)
// declare your public methods
@end
Now, when you generate the new subclasses from the model, all of these will still work.
Related Topics
Linking Error When Building Parse in Xcode 7
Auth.Auth().Currentuser.Reload() Doesn't Refresh Currentuser.Isemailverified
Accessing Struct from One Class to Another
Declaring Conformance to @Objc Protocol in Empty Extension Breaks with Exc_Bad_Instruction
How Does One Use Nsdateformatter's Islenient Option
Swift Implement Literalconvertible Protocol
Cannot Preview This File, App May Have Crashed -- Occurs When Inputting Specific Line of Code
How to Make Struct Lazylist in Swiftui
Passing Unknown Number of Arguments in a Function
How to Call Swiftui Navigationlink Conditionally
Nsurl from String Gets Truncated, Dots on the Uivideoeditorcontroller's Video Path Swift 2 iOS 8
Empty Class in Swift Playground Gives _Lldb_Expr_ Error
Swift Spritekit 3D Touch and Touches Moved
Swift 2: Multiline Mkpointannotation
Tabview, Tabitem: Running Code on Selection or Adding an Ontapgesture
Equivalent of Skaction Scaletox for a Given Duration in Unity