![](/img/trans.png)
[英]How to(Can I) get the content of other section from a NSFetchedResultsController?
[英]How can I get number of objects in section, NSFetchedResultsController Swift
如何在Swift中的NSFetchedResultcController部分获取对象数?
if let s = self.fetchedResultsController?.sections as? NSFetchedResultsSectionInfo {
}
给我的Cannot downcast from '[AnyObject]' to non-@objc protocol type NSFetchedResultsSectionInfo
var d = self.fetchedResultsController?.sections[section].numberOfObjects
给我does not have member named 'subscript'
你需要投self.fetchedResultsController?.sections
到Array
的NSFetchedResultsSectionInfo
对象:
if let s = self.fetchedResultsController?.sections as? [NSFetchedResultsSectionInfo]
然后你可以将该section
传递给下标并获得对象的数量:
if let s = self.fetchedResultsController?.sections as? [NSFetchedResultsSectionInfo] {
d = s[section].numberOfObjects
}
我认为Mike S目前接受的答案是Swift 2.0
以下是为我工作(Swift 2.1):
if let sections = fetchedResultsController?.sections {
return sections[section].numberOfObjects
} else {
return 0
}
这是我在UITableViewController
(Swift 4)中的内容:
override func numberOfSections(in tableView: UITableView) -> Int {
guard let sections = fetchedResultsController.sections else { return 0 }
return sections.count
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.