CoreData:错误:NSFetchedResultsController:在索引0的部分中索引0处没有对象

时间:2019-08-04 10:52:52

标签: ios swift core-data nsfetchedresultscontroller

我不明白为什么coredata会引发此错误,我试图从plist中获取数据并一次又一次地收到此错误。

private func configureFetchedRresultsController(key : String, entity : String) {
    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
        return
    }

    let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: entity)
    let sortDescriptor = NSSortDescriptor(key: key, ascending: false)
    fetchRequest.sortDescriptors = [sortDescriptor]

    fetechedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: appDelegate.persistentContainer.viewContext, sectionNameKeyPath: nil, cacheName: nil)
    fetechedResultsController?.delegate = self
    do {
        try fetechedResultsController?.performFetch()
    } catch {
        print(error.localizedDescription)
    }
}



func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

     guard let sections = fetechedResultsController?.sections else {
        return 0
    }

    let rowCount = sections[section].numberOfObjects
    return rowCount

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CategoryCollectionViewCell
    if let cat = fetechedResultsController?.object(at: indexPath) as? Categories {
         cell.categoryName.text = cat.category
        cell.categoryImg.image = image1
     cell.categoryName.text = cat.desc
        //cell.categoryDes.text = catogries[indexPath.item].desc

    }

0 个答案:

没有答案