infernouk infernouk - 8 days ago 16
Swift Question

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

Answer

The current default in Xcode is to automatically create a subclass of NSManagedObject for you in your projects directory. 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 manually create a subclass of NSManagedObjects what you need to do is:

  1. Create your entities
  2. Select each entity and under attributes inspector choose "Manual/None" in the Codegen dropdown menu.
  3. Create a subclass of NSManagedObject by going to Editor>Create NSManagedObject Subclass...

Hope this helped!

Comments