2017-07-18 29 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 
    } 

希望這會幫助你