ios - NSFetchedResultsController count in Swift -
i novice swift developer coming objective-c background.
i have following code:
class kpcoredataresultscontroller: nsobject, nsfetchedresultscontrollerdelegate { var fetchedresultscontroller: nsfetchedresultscontroller let managedobjectcontext = (uiapplication.sharedapplication().delegate as! appdelegate).managedobjectcontext! let entityname: nsstring var delegate: kpcoredataresultsdelegate? var predicate: nspredicate { { return fetchedresultscontroller.fetchrequest.predicate! } set { fetchedresultscontroller.fetchrequest.predicate = newvalue { try fetchedresultscontroller.performfetch() } catch { print(error) } } } var sortdescriptor: nssortdescriptor! var count: nsinteger { return (fetchedresultscontroller.sections!.first!).numberofobjects } init(entityname: string, predicate: nspredicate, sortdescriptor: nssortdescriptor) { self.entityname = entityname let fetchrequest = nsfetchrequest(entityname: entityname) fetchrequest.predicate = predicate fetchrequest.sortdescriptors = [sortdescriptor] fetchedresultscontroller = nsfetchedresultscontroller(fetchrequest: fetchrequest, managedobjectcontext: managedobjectcontext, sectionnamekeypath: nil, cachename: nil) super.init() } }
inside of custom core data results controller file simplify accessing coredata stuff. when call results controller's count property table view (numberofrowsinsection)
, app crashes saying:
fatal error: unexpectedly found nil while unwrapping optional value
here table view code:
override func tableview(tableview: uitableview, numberofrowsinsection section: int) -> int { return accountcontroller.count }
basically, code above giving error - using !'s wrong? or happening here?
try
return fetchedresultscontroller.sections![section].numberofobjects
Comments
Post a Comment