Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Errors after 'Create NSManagedObject Subclass for CoreData Entities

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.

enter image description here

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

like image 226
infernouk Avatar asked Nov 23 '16 16:11

infernouk


Video Answer


3 Answers

Xcode by default manage generation of these subclasses for you. If you want to manage generation of them yourself then:

  1. Select your entities in the data model.
  2. Find Codegen in the utilities pane(right pane) and set it to Manual/None.
  3. Clean and build.
like image 67
Niklas Berglund Avatar answered Sep 27 '22 21:09

Niklas Berglund


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-agkwmibzbo‌​pevjgfajlcbyixzyev/B‌​uild/Intermediates/A‌​ppName.build/Debug-i‌​phonesimulator/AppNa‌​me.build/DerivedSour‌​ces/CoreDataGenerate‌​d/Modeldirectory; 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:

  1. Open terminal and navigate to /Users/<your user name>/Library/Developer/Xcode/DerivedData/AppName-agkwmibzbo‌​pevjgfajlcbyixzyev/B‌​uild/Intermediates/A‌​ppName.build/Debug-i‌​phonesimulator/AppNa‌​me.build/DerivedSour‌​ces/CoreDataGenerate‌​d/Model

  2. 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)

  3. Delete your current data model

  4. Create a new data model

  5. 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.

  6. Create a subclass of NSManagedObject by going to Editor>Create NSManagedObject Subclass...

Your errors should disappear.

Hope this helped!

like image 31
fja Avatar answered Sep 27 '22 20:09

fja


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.

like image 36
Ammad Avatar answered Sep 27 '22 20:09

Ammad