Sylvain
Sylvain

Reputation: 33

CoreData: error: Failed to call designated initializer on NSManagedObject class

I did some research and I really don't understand what happened here. I have this error when I select a row in a table view :

Wish[1392:37721] CoreData: error: Failed to call designated initializer on NSManagedObject class 'Wish.ProduitEntity' (lldb)

The error is on the prepareForSegue method in ViewController class.

Thanks for the help

import UIKit
import CoreData

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var leTbleView: UITableView!

var arrayProduit = [ProduitEntity]()

var produitSelectionne : ProduitEntity? = nil

override func viewDidLoad() {
    super.viewDidLoad()
    self.leTbleView.dataSource = self
    self.leTbleView.delegate = self
}

override func viewWillAppear(animated: Bool) {
    let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
    let request = NSFetchRequest(entityName: "ProduitEntity")
    var ilStock = [AnyObject]?()

    do{
        try ilStock = context.executeFetchRequest(request)
    } catch _ {
    }


    //put info in the tableView
    if ilStock != nil {
        arrayProduit = ilStock as! [ProduitEntity]
    }
    self.leTbleView.reloadData()
}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return arrayProduit.count
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = UITableViewCell()
    cell.textLabel!.text = (arrayProduit[indexPath.row]).nom
    return cell
}


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    produitSelectionne = self.arrayProduit[indexPath.row]
    performSegueWithIdentifier("detailSegue", sender: self)

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "detailSegue" {
        let detailVC = segue.destinationViewController as! DetailViewController
        detailVC.uneInstanceEntity = self.produitSelectionne!}
}
}



import UIKit
import CoreData

class DetailViewController: UIViewController {

@IBOutlet weak var titleLbl: UILabel!

@IBOutlet weak var storeLbl: UILabel!

@IBOutlet weak var imageProduit: UIImageView!



var uneInstanceEntity = ProduitEntity()

override func viewDidLoad() {
    super.viewDidLoad()
    self.titleLbl.text = uneInstanceEntity.nom
    self.storeLbl.text = uneInstanceEntity.magasin

}
}



import UIKit
import CoreData

class ajouterProduitViewController: UIViewController {

@IBOutlet weak var modelTxtField: UITextField!

@IBOutlet weak var magasinTxtField: UITextField!

@IBOutlet weak var photoImage: UIImageView!


override func viewDidLoad() {
    super.viewDidLoad()

}

//Add a new product 
func sauvegardeProduit() {
    let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
    let objetEntity = NSEntityDescription.insertNewObjectForEntityForName("ProduitEntity", inManagedObjectContext: context) as!ProduitEntity

    objetEntity.nom = modelTxtField.text
    objetEntity.magasin = magasinTxtField.text
    //objetEntity.unVisuel = UIImageJPEGRepresentation(UIImage(named: ""), 1)

    do {
        try context.save()
    } catch _ {
    }
}


@IBAction func saveBtn(sender: AnyObject) {
    sauvegardeProduit()
    self.dismissViewControllerAnimated(true, completion: nil)

}

@IBAction func cnclBtn(sender: AnyObject) {
    self.dismissViewControllerAnimated(true, completion: nil)
}

}

Upvotes: 2

Views: 5885

Answers (1)

Wain
Wain

Reputation: 119021

The problem is this line:

var uneInstanceEntity = ProduitEntity()

Because you're directly creating an instance. You should't do that, you should make it optional or forced:

var uneInstanceEntity: ProduitEntity!

Upvotes: 15

Related Questions