I have created 2 core data entities and then created NSManagedObject Subclasses for them from the editor menu.
However when I run my app I get errors on every line of all the files for some reason.
Here is an example, these errors are the same for both entities created files.
File Code was auto generated so i can apste it here but not sure of its use
import Foundation
import CoreData
extension UserExercise {
@nonobjc public class func fetchRequest() -> NSFetchRequest<UserExercise> {
return NSFetchRequest<UserExercise>(entityName: "UserExercise");
}
@NSManaged public var id: Int16
@NSManaged public var name: String?
@NSManaged public var reps: Int16
@NSManaged public var sets: Int16
@NSManaged public var weight: Int16
@NSManaged public var relationship: NSSet?
}
// MARK: Generated accessors for relationship
extension UserExercise {
@objc(addRelationshipObject:)
@NSManaged public func addToRelationship(_ value: UserRoutine)
@objc(removeRelationshipObject:)
@NSManaged public func removeFromRelationship(_ value: UserRoutine)
@objc(addRelationship:)
@NSManaged public func addToRelationship(_ values: NSSet)
@objc(removeRelationship:)
@NSManaged public func removeFromRelationship(_ values: NSSet)
}
Errors are:
Command/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1
Invalid redeclaration of 'UserRoutine' 'UserExercise' is ambiguous for type lookup in this context @NSManaged only allowed on an instance property or method
Theres too many to list its basically these repeated over and over
Xcode by default manage generation of these subclasses for you. If you want to manage generation of them yourself then:
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!
A simple approach that worked for me .... You can find these by choosing an entity and clicking on the Data Model Inspector at the top right . Do this for all of your entities
Its important to FIRST set Module to "Current Product Module" AND Codegen to "Manual/None"
Only then, SECOND: Editor -> Create NSManagedObject subclass.
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