How can I get number of objects in section of an NSFetchedResultcController in Swift?
if let s = self.fetchedResultsController?.sections as? NSFetchedResultsSectionInfo {
}
is giving me Cannot downcast from '[AnyObject]' to non-@objc protocol type NSFetchedResultsSectionInfo
var d = self.fetchedResultsController?.sections[section].numberOfObjects
gives me does not have member named 'subscript'
You need to cast self.fetchedResultsController?.sections
to an Array
of NSFetchedResultsSectionInfo
objects:
if let s = self.fetchedResultsController?.sections as? [NSFetchedResultsSectionInfo]
Then you can pass the section
to the subscript and get the number of objects:
if let s = self.fetchedResultsController?.sections as? [NSFetchedResultsSectionInfo] {
d = s[section].numberOfObjects
}
I think the currently accepted answer by Mike S was pre Swift 2.0
The following is working for me (Swift 2.1):
if let sections = fetchedResultsController?.sections {
return sections[section].numberOfObjects
} else {
return 0
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With