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.
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!
Cannot create NSManagedObject subclass
You are manually generating a NSManagedObject
subclass, which has already been generated by Xcode.
Find detailed solutions here.
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")
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 generate NSManagedObject Subclass issue
So I also ran in this issue. You have to disable automatical code generation if you want to create the NSManagedObjects by yourself. So there are different ways. First way is go to your model and changed the Toolversion to xCode 7.3.
In every entity you can set the Codegen to Manual/None like in the Screenshot.
Clean your project, and try again!
I hope this was helpful.
Related Topics
Using Stringbyreplacingcharactersinrange in Swift
Parsing a Iso8601 String to Date in Swift
Swift - Cast Int64 to Anyobject for Nsmutablearray
How to Return a First Word from a String in Swift
Why Can the Keyword "Weak" Only Be Applied to Class and Class-Bound Protocol Types
Writing Swift Dictionary to File
How to Compare "Any" Value Types
Get Current Time as String Swift 3.0
How to Have a Swift Script Use Multiple Files
How to Write Inline Assembly in Swift
Swift 4 Decode Simple Root Level JSON Value
How to Detect Absent Network Connection When Setting Firestore Document
How to Cast Any to an Optional
Coreplot with Swift: There Is No Yaxis.Majorintervallength
How to Avoid Nested Navigation Bars in Swiftui