2014-09-21 14 views
6

如何获取Swift中NSFetchedResultcController节的对象数?如何获取节中的对象数NSFetchedResultsController Swift

if let s = self.fetchedResultsController?.sections as? NSFetchedResultsSectionInfo { 

    } 

是给我Cannot downcast from '[AnyObject]' to [email protected] protocol type NSFetchedResultsSectionInfo

var d = self.fetchedResultsController?.sections[section].numberOfObjects 

给我does not have member named 'subscript'

回答

11

你需要投self.fetchedResultsController?.sectionsNSFetchedResultsSectionInfoArray对象:

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

然后,你可以通过section到t他下标并获得对象的数量:

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

我认为麦克S中目前公认的答案是预雨燕2.0

以下是为我工作(雨燕2.1):

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