我做了一些研究,我真的不明白这里发生了什么。 当我在表视图中选择一行时出现此错误:
愿望[1392:37721] CoreData:错误:无法在NSManagedObject类'Wish.ProduitEntity'上调用指定的初始值设定项 (lldb)
错误发生在ViewController类的prepareForSegue方法中。
感谢您的帮助
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)
}
}
答案 0 :(得分:8)
问题在于这一行:
var uneInstanceEntity = ProduitEntity()
因为您正在直接创建实例。你不应该这样做,你应该把它变成可选的或强制的:
var uneInstanceEntity: ProduitEntity!