Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't instantiate subclass of NSManagedObject

Xcode 6 has had a ton of bugs. But I'm not quite sure if this is a bug or not. It might not be since this is something I'm just now learning.

My issue is, any time I try to instantiate my subclass of NSManagedObject, I do not have the option to pass the entity: NSEntityDescription and NSManagedContext: insertIntoManagedContext argument to the constructor, Xcode says "Extra Argument 'entity' in call"

I created a new Xcode project from scratch, just to see if I could re-create the problem in a smaller, minimal project.

ToDoList.Item is set as the Item entity class in the Data Model Inspector.

Here's the code:

override func viewDidLoad() {

    super.viewDidLoad()

    let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    let context: NSManagedObjectContext = appDel.managedObjectContext!

    let ent = NSEntityDescription.entityForName("Item", inManagedObjectContext: context)!

    //compiler complains here
    var item = Item(entity: ent, insertIntoManagedObjectContext: context)!

}

Here's the subclass:

import UIKit
import CoreData

class Item: NSManagedObject {

    @NSManaged var title: String
    @NSManaged var completed: Bool

}

All help is appreciated.

like image 844
Ryan Smith Avatar asked Oct 28 '14 01:10

Ryan Smith


People also ask

How do I create a subclass in NSManagedObject?

From the Xcode menu bar, choose Editor > Create NSManagedObject Subclass. Select your data model, then the appropriate entity, and choose where to save the files. Xcode places both a class and a properties file into your project.

What is NSManagedObject in ios?

In some respects, an NSManagedObject acts like a dictionary—it's a generic container object that provides efficient storage for the properties defined by its associated NSEntityDescription instance.

What is NSManagedObject context?

An object space to manipulate and track changes to managed objects.


2 Answers

Just came across the same problem: Init method for core data entity not available

Obviously we have to implement the

init(entity: NSEntityDescription, insertIntoManagedObjectContext context, NSManagedObjectContext?)

method in our custom NSManagedObject class. So just add

override init(entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext?) {
    super.init(entity: entity, insertIntoManagedObjectContext: context)
}

to your entity class and it will work.

like image 90
UpCat Avatar answered Oct 06 '22 22:10

UpCat


Try the final line without exclamation mark, like this:

var item = Item(entity: ent, insertIntoManagedObjectContext: context)

And maybe You haven't added your app name to class name:

Swift classes are namespaced—they’re scoped to the module (typically, the project) they are compiled in. To use a Swift subclass of the NSManagedObject class with your Core Data model, prefix the class name in the Class field in the model entity inspector with the name of your module.

Class namehttps://developer.apple.com/library/mac/documentation/Swift/Conceptual/BuildingCocoaApps/WritingSwiftClassesWithObjective-CBehavior.html

like image 25
Viesturs Knopkens Avatar answered Oct 06 '22 22:10

Viesturs Knopkens