Ios NSFetchedResultsController-在展开可选值时意外发现nil
我正在使用NSFetchedResultsController填充我的TableView。我有一个属性为Name&HubID的实体hubbrofile 问题:NSFetchedResultsController将为零。奇怪的是,当我在viewDidLoad&cellForRowIndexPath方法中打印fetchedResultsController时,它给出了一个值。但是在numberOfRowsInSection方法中,fetchedResultsController为nil,应用程序崩溃 此外,数据已保存在CoreData中。我在SQLite浏览器中看到了它-因此有数据要加载 我似乎不明白为什么 下面是我的代码:Ios NSFetchedResultsController-在展开可选值时意外发现nil,ios,swift,nsfetchedresultscontroller,nsfetchrequest,Ios,Swift,Nsfetchedresultscontroller,Nsfetchrequest,我正在使用NSFetchedResultsController填充我的TableView。我有一个属性为Name&HubID的实体hubbrofile 问题:NSFetchedResultsController将为零。奇怪的是,当我在viewDidLoad&cellForRowIndexPath方法中打印fetchedResultsController时,它给出了一个值。但是在numberOfRowsInSection方法中,fetchedResultsController为nil,应用程序崩溃
class StudentsController: UIViewController, UITableViewDataSource, UITableViewDelegate, NSFetchedResultsControllerDelegate
{
let managedContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
var fetchedResultsController: NSFetchedResultsController!
override func viewDidLoad() {
super.viewDidLoad()
//FETCH REQUESTS
let fetchRequest = NSFetchRequest(entityName: "HubProfile")
let sortDescriptor = NSSortDescriptor(key: "name", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
fetchRequest.predicate = NSPredicate(format: "hubID = %@", hubID!)
fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedContext, sectionNameKeyPath: nil, cacheName: nil)
fetchedResultsController.delegate = self
do
{
try! fetchedResultsController.performFetch()
}
print(fetchedResultsController) //THIS IS NOT NIL
}
//TABLEVIEWDATASOURCE PROTOCOL:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(fetchedResultsController) // THIS IS NIL
let sectionInfo = fetchedResultsController.sections![section] as NSFetchedResultsSectionInfo
return sectionInfo.numberOfObjects
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! StudentsCell!
print(fetchedResultsController) //THIS is NOT Nil
let hubProfile = fetchedResultsController.objectAtIndexPath(indexPath) as! HubProfile
cell.nameLabel?.text = hubProfile.name
return cell
}}
在fetchResultsController有机会加载之前,tableView可能已经加载。因此,在执行以下操作后,请尽快重新加载表格:
do
{
try! fetchedResultsController.performFetch()
tableView.reloadData()
}
print(fetchedResultsController) //THIS IS NOT NIL
}
然后,在tableView函数中,检查fetchedResultsController是否为nil,如果为nil,则在本例中为其指定一些其他值,0。当完成提取后重新加载表时,函数将从fetchResultsController中获取项目零:
override func numberOfSections(in tableView: UITableView) -> Int {
return fetchedResultsController.sections?.count ?? 0
}
所以表视图试图在任何数据出现之前访问“fetchedResultsController”。所以,您可以做的是首先检查它是否为空,然后在获取viewdidload后放入tableview.reload。当提取数据时,它将重新加载tableview。无论何时使用可选值,尝试安全地展开,则不会出现此错误。尝试重新加载数据..不起作用..正如我在viewDidLoad中所说..我已经放置了printfetchedResultsController..工作正常..问题只存在于numberOfRowsInSection方法中。将viewDidLoad设置为可选并进行以下所有更改可以解决此问题,出于好奇,我想看看tableView:numberOfRowsInSection:call的回溯,其中fetchedResultsController为零,以及它是在viewDidLoad之前还是之后调用的。我不确定我是否理解正确..我将fetchedResultsController声明设置为可选..它仍然崩溃…因为u NSArray0 objectAtIndex::索引0超出空NSArray的界限'…基本上fetchedResultsController为零。。