I am trying to convert a working table view to a table view making use of NSFetchedResultsController.
I am testing with just two entries and this is part of my working code
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let fetchRequest = NSFetchRequest(entityName: "Appointments")
//3
do {
self.tableView.reloadData()
let results = try context.executeFetchRequest(fetchRequest)
invoices = results as Array<AnyObject> //cast to Appointments didn't work
} catch let error as NSError {
print("Could not fetch \(error), \(error.userInfo)")
}
And this is part of my converting code
let context = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
var frc: NSFetchedResultsController = NSFetchedResultsController()
func getFetchedResultsController()->NSFetchedResultsController {
frc = NSFetchedResultsController(fetchRequest: listFetchRequest(), managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
return frc
}
func listFetchRequest()->NSFetchRequest{
let fetchRequest = NSFetchRequest(entityName: "Appointments")
let sortDescriptor = NSSortDescriptor(key: "appointmentUser", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
return fetchRequest
}
var appointments = [AnyObject]()//[NSManagedObject]()
It builds successfull, but i am directly getting an error message: fatal error: unexpectedly found nil while unwrapping an Optional value
libswiftCore.dylib`function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded, Arg[2] = Dead, Arg[3] = Dead> of Swift._fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> ():
0x1002c0a50 <+0>: stp x29, x30, [sp, #-16]!
0x1002c0a54 <+4>: mov x29, sp
0x1002c0a58 <+8>: sub sp, sp, #16
0x1002c0a5c <+12>: and w8, w2, #0x1
0x1002c0a60 <+16>: tbnz w8, #0, 0x1002c0a80 ; <+48>
0x1002c0a64 <+20>: tbnz x1, #63, 0x1002c0ac4 ; <+116>
0x1002c0a68 <+24>: add x1, x0, x1
0x1002c0a6c <+28>: mov x2, x3
0x1002c0a70 <+32>: mov x3, x4
0x1002c0a74 <+36>: mov x4, x5
0x1002c0a78 <+40>: bl 0x10030d910 ; function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded> of Swift.(_fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> ()).(closure #2)
-> 0x1002c0a7c <+44>: brk #0x1
0x1002c0a80 <+48>: str xzr, [sp, #8]
0x1002c0a84 <+52>: cmp x0, w0, uxtw
How can i check where the unwrapping occurs? How can i fix this?
[EDIT] With some trial and error (outcommenting parts) i think it has to do with this part of my code
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
// return 1
let numberOfSections = frc.sections?.count
return numberOfSections!
}