【问题标题】:How can I get number of objects in section, NSFetchedResultsController Swift如何在 NSFetchedResultsController Swift 部分中获取对象数量
【发布时间】:2014-09-21 02:44:54
【问题描述】:

如何在 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'

【问题讨论】:

    标签: ios swift protocols nsfetchedresultscontroller


    【解决方案1】:

    您需要将self.fetchedResultsController?.sections 转换为ArrayNSFetchedResultsSectionInfo 对象:

    if let s = self.fetchedResultsController?.sections as? [NSFetchedResultsSectionInfo]
    

    那么就可以把section传给下标,得到对象的个数:

    if let s = self.fetchedResultsController?.sections as? [NSFetchedResultsSectionInfo] {
        d = s[section].numberOfObjects
    }
    

    【讨论】:

      【解决方案2】:

      我认为 Mike S 目前接受的答案是 Swift 2.0 之前的版本

      以下内容对我有用(Swift 2.1):

      if let sections = fetchedResultsController?.sections {
          return sections[section].numberOfObjects
      } else {
          return 0
      }
      

      【讨论】:

        【解决方案3】:

        这就是我的 UITableViewController (Swift 4) 中的内容:

        override func numberOfSections(in tableView: UITableView) -> Int {
            guard let sections = fetchedResultsController.sections else { return 0 }
            return sections.count
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-09
          • 2015-10-27
          • 2017-08-13
          相关资源
          最近更新 更多