2017-07-18 92 views
3

我正在使用核心数据将信息保存到对象,但是当我尝试使用它时,程序崩溃并说“致命错误:在解包可选值时意外发现零”我使用coreData保存数据,但对象保持无

这就是我产生

func generateTestData() 
{ 
    let item = Item(context: context) 
    item.title = "New Iphone 7s" 
    item.price = 2000 
    item.details = "I wish it's something that is worth to apple , unline Iphone 7" 


    let item2 = Item(context: context) 
    item2.title = "Beach House in France" 
    item2.price = 3000000 
    item2.details = "I will live there for the rest of my Life , untill then i don't have a life" 
} 

这是取功能

func attemptFetch() 
{ 
    let fetchRequest :NSFetchRequest<Item> = Item.fetchRequest() 
    let datasort = NSSortDescriptor(key: "created", ascending: false) 
    fetchRequest.sortDescriptors = [datasort] 
    let controller = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil) 
    self.controller = controller 

    do{ 
     try controller.performFetch() 
    } 
    catch{ 
     let error = error as NSError 
     print(error.debugDescription) 
    } 
} 

坠机发生在这里,当我尝试更新我的视图中的数据

func configureCell(cell : objectItemCell , indexPath :IndexPath) 
{ 
    let item = controller.object(at:indexPath) 
    cell.configureCell(item: item) 
} 

UITableViewCell类,

func configureCell(item : Item) 
{ 
    self.title.text = item.title 
    self.price.text = "$\(item.price)" 
    self.details.text = item.details 
} 
+0

这意味着什么是零。试着找出哪个属性是零,哪条线正是导致崩溃。 – Larme

+0

那个说self.title.text = item.title,其余的同样去下 –

+0

但是这是否意味着'item'是零?那个'item.title'是零,或者'self.title'是零?这是不同的情况,无论这个问题是在CoreData端还是在Cell端。 – Larme

回答

2

从项目获取数据之前,请保存上下文。在你的情况下,在generateTestData(),做context.save(),可能是你的应用程序崩溃,因为你不保存数据,并试图获取其返回nil,iOS中

func generateTestData() 
{ 
let item = Item(context: context) 
item.title = "New Iphone 7s" 
item.price = 2000 
item.details = "I wish it's something that is worth to apple , unline Iphone 7" 


let item2 = Item(context: context) 
item2.title = "Beach House in France" 
item2.price = 3000000 
item2.details = "I will live there for the rest of my Life , untill then i don't have a life" 
saveContext() // save data that you initialised 
} 

// MARK: - Core Data Saving support 
func saveContext() { 
    if #available(iOS 10.0, *) { 
     let context = persistentContainer.viewContext 
     if context.hasChanges { 
      do { 
       try context.save() 
      } catch { 
       // Replace this implementation with code to handle the error appropriately. 
       // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
       let nserror = error as NSError 
       fatalError("Unresolved error \(nserror), \(nserror.userInfo)") 
      } 
     } 
    } else { 
     // Fallback on earlier versions 
     if managedObjectContext.hasChanges { 
      do { 
       try managedObjectContext.save() 
      } catch { 
       // Replace this implementation with code to handle the error appropriately. 
       // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
       let nserror = error as NSError 
       NSLog("Unresolved error \(nserror), \(nserror.userInfo)") 
       abort() 
      } 
     } 
    } 

} 
+0

仍然崩溃,当我调试数据被保存,但当我尝试使用它崩溃 –

0

至于核心数据关心的是,当你使用generateTestData()时,数据是在NSManagedObjectContext中创建的。仅仅因为它驻留在NSManagedObjectContext中并不意味着它被保存在SQLite中。

enter image description here

将数据保存到SQLite的(持久性需要,这样你就不用跑了generateTestData()每次),使用

ad.saveContext() 

的saveContext()就像是一个提交语句以分贝计。为了使用上述内容,请在类定义之外的AppDeligate.swift中声明以下内容,以便在控制器中访问您的上下文。

let ad = UIApplication.shared.delegate as! AppDelegate 
let contextAP = ad.persistentContainer.viewContext 

NOTE : Doing the above saves the data in SQLite but running generateTestData() twice with same data with saveContext() will create duplicate records in your SQLite.

图像参考:https://www.objc.io/images/issue-4/stack-simple-9af1e89d.png

0

我想你忘了在你试图获取您的对象类来创建一个managedObjectContext /实体来说

import CoreData 

sampleClass: UITableViewController, UITableViewDataSource ... { 

    var context: NSManagedObjectContext! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let appDelegate = UIApplication.shared.delegate as! UIAppDelegate 
     context = appDelegate.persistentContainer.viewContext 
    } 

    funcAttemptFetch() { 
     // your code 
    } 

希望这会帮助你