I save data using coreData but yet objects stay nil

Viewed 850

I am using core data to save information to objects, but when I try to use it the program crashed and says "fatal error: unexpectedly found nil while unwrapping an Optional value"

This is the data which I generate

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"
}

this is the fetch function

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)
    }
}

the crash happens here when i try to update my view

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

UITableViewCell class

func configureCell(item : Item)
{
    self.title.text = item.title
    self.price.text = "$\(item.price)"
    self.details.text = item.details
}
3 Answers
Related