20

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

Community
  • 1
  • 1
infernouk
  • 1,137
  • 4
  • 13
  • 21
  • Th error message *extensions may not contain stored properties* is pretty clear: You cannot declare `@NSManaged` properties in a class extension. – vadian Nov 23 '16 at 16:11
  • 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 Nov 23 '16 at 16:13
  • if you created the subclasses yourself you must set the `Codegen` popup in the Core Data model to *Manual/None* – vadian Nov 23 '16 at 16:17
  • 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 Nov 23 '16 at 21:14
  • http://stackoverflow.com/questions/40351449/invalid-redeclaration-in-auto-code-generate-nsmanagedobject-subclass-swift-3 – Pranav Jaiswal Dec 02 '16 at 00:19
  • Possible duplicate of [invalid redeclaration in auto code generate NSManagedObject Subclass Swift 3](https://stackoverflow.com/questions/40351449/invalid-redeclaration-in-auto-code-generate-nsmanagedobject-subclass-swift-3) – mfaani Dec 16 '18 at 22:04

7 Answers7

27

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.
Niklas Berglund
  • 3,563
  • 4
  • 32
  • 32
  • 1
    Accepted answer is well explained. But I prefer yours better. It's short and straight to the point – KMC Mar 15 '17 at 19:07
  • OP's problem was not solvable by just clean building the project. When the question was asked, sometimes Xcode did not delete all files. That was OP's original problem. It may have been a bug when the question was asked and resolved in subsequent versions of Xcode. @user30646 – fja Apr 10 '17 at 14:50
17

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!

fja
  • 1,823
  • 1
  • 15
  • 28
  • so the error was because i created the subclasses without setting the attribute to Manual/None? – infernouk Nov 23 '16 at 21:13
  • Well that depends... Did all your errors go away? :P But yes the new Xcode creates NSManagedObject subclasses automatically and if you wish to create subclasses of NSManagedObject the old way (aka doing Editor>Create NSManagedObject Subclass...) then you must first set the Codegen for each entity to manual. Also, if this helped don't forget to choose my answer :) @infernouk – fja Nov 23 '16 at 21:44
  • 1
    wait, it creates them automatically so i dont even need to do "Create NSManagedObject Subclass" I cant see any files created automatically? i can just make an entity and carry on? I tried deleting the files it had created, setting the entities to manual/none and then creating them again with editor>create, but it gave me the same errors! – infernouk Nov 23 '16 at 22:37
  • The files in question are not stored in the project directory but instead are **deeply** nested in the "DerivedData" directory. Since you want to see them, open the terminal and navigate to this directory: /Users//Library/Developer/Xcode/DerivedData/AppName-agkwmibzbopevjgfajlcbyixzyev/Build/Intermediates/AppName.build/Debug-iphonesimulator/AppName.build/DerivedSources/CoreDataGenerated/Model – fja Nov 24 '16 at 03:58
  • delete all files in the above directory by running rm -fr * (now be careful with this command only run it when you get to the final directory where the generated codes are or you'll break your project for good) when you have deleted all the generated file only then you can start fresh. Create an entity then set the Codegen to "manual" **before** the first run after you have created a new model. – fja Nov 24 '16 at 04:04
  • I have till this /Users//Library/Developer/Xcode/DerivedData/AppName-agkwmibzbo‌​pevjgfajlcbyixzyev/B‌​uild/Intermediates/A‌​ppName.build/Debug-i‌​phonesimulator/AppNa‌​me.build/DerivedSour‌​ces/-Swift.h . There is no CoreDataGenerate‌​d/Model in it. How is that ? What to do now? I tried all fixes but no fix helped. Do you have any other fix for this issue ? – Sushree Swagatika May 03 '17 at 14:19
  • @SushreeSwagatika run `ls -la` on the command line when you reach the final directory and see if they show up, the files might be hidden. let me know. – fja May 03 '17 at 14:47
  • It got fixed. I had created a separate NSObject class for my entities. Hence was showing duplicate declaration / re-declaration of File. After removing them errors got removed. Thanks @A.Jam – Sushree Swagatika May 04 '17 at 09:09
  • If my answer helped don't forget to up vote it ;) and you are welcome @SushreeSwagatika – fja May 04 '17 at 12:19
7

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.

goto
  • 7,908
  • 10
  • 48
  • 58
Ammad
  • 341
  • 6
  • 7
  • @SushreeSwagatika it worked for me always not sure what ur exact problem is ...note that in latest version of xcode u dont need to create these files manually Xcode does it for you automatically ... if u still need to do something with your entities u can create an extension for those – Ammad May 05 '17 at 07:28
  • yes u r correct. It was not working due to another issue. Thanks. – Sushree Swagatika May 05 '17 at 09:42
3

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.

Rob
  • 338
  • 3
  • 5
  • 1
    Because sometimes Xcode does / did not delete all files. That was OP's original problem. It may have been a bug when the question was asked and resolved in subsequent versions of Xcode. – fja Apr 10 '17 at 14:47
2

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

Joshua Cleetus
  • 624
  • 6
  • 14
0

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.

bitwit
  • 2,589
  • 2
  • 28
  • 33
0

I was getting the "... is ambiguous for type lookup in this context" error when building a simple SwiftUI Mac app for testing this issue after running Editor/Create NSManagedObject subclass..." with Xcode Version 13.4 (13F17a) on macOS Monterey 12.3.1.

I found a solution in deselecting/unchecking the checkbox for "Targets" in the final dialog before clicking "Create". This checkbox was selected by default. Clearing it and clicking "Create" resulted in a successful build.

Picture showing where to uncheck the checkbox for Targets.

This results with the subclass files at the top level of the project structure:

Picture showing successfull build with subclass files at top level of project structure.

Mojo Techno
  • 96
  • 1
  • 4