20
votes

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

6
Th error message extensions may not contain stored properties is pretty clear: You cannot declare @NSManaged properties in a class extension. - vadian
This wasnt anything to do with me though, these are auto generated files from swift from an entity? why would it generate broken error filled files? I have watched tutorials using this method and their files generated without issue - infernouk
if you created the subclasses yourself you must set the Codegen popup in the Core Data model to Manual/None - vadian
By created subclasses myself how do you mean? As i used the swift auto generated option ion editor, i didnt make these files myself, just built the entities in the xcdatamodelid - infernouk

6 Answers

25
votes

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.
16
votes

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!

7
votes

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.

2
votes

This is for Swift 3.0 and Xcode 8.2.1(8C1002)

Not sure why you would manually navigate to the project when you can just instead delete the files directly from Xcode.

Find all of your generated files from the xcdatamodeld, inside of your project navigator. Delete all of the files and make sure you move to trash, do not remove references otherwise you're going to have to manually delete the files from:

/Users//Library/Developer/Xcode/DerivedData/AppName-agkwmibzbo‌​pevjgfajlcbyixzyev/B‌​uild/Intermediates/A‌​ppName.build/Debug-i‌​phonesimulator/AppNa‌​me.build/DerivedSour‌​ces/CoreDataGenerate‌​d/Model

Ok so after you've deleted the files, open your xcdatamodeld, select all of your entities, and in the Utilities panel, select Data Model Inspector button, and make sure that Codegen is set to "Manual/None".

Select your xcdatamodeld and make sure that that Codegen is set to "Manual/None"

Keep all of your entities selected and click Editor > Create NSManagedObject Subclass, make sure to select the folder and drag and drop your generated files in your Model sub-group and your Generated sub-group.

This worked for me and it should work for you too.

I hope that I have helped you.

2
votes

I also ran into this bug. I fixed it by deleting the old generated entity subclasses and creating it again and for the group I selected the workspace instead of the project or folder in the project, and the subclasses got saved above the project file. It looks weird but it fixed the issue. Also if i move the files into the folder inside the project the error reappears.enter image description here

0
votes

I ran into this issue during unit testing due to recreating my NSPersistentContainer over and over again in every unit test. It looks like loading the same managed object model repeatedly could lead to this.

My resolution was to use a single shared instance across all of my unit tests and do clean up between them accordingly.