假设我有一个这样的数据库表:
| id | gender| |------|-------| | 0001 | Male | | 0002 | Male | | 0003 | Female| | 0004 | Female|
And I use NSFetchedResultsController with "id" as NameKeyPath to fetch four sections:
var fetchedID = NSFetchedResultsController(fetchRequest: fetchRequest,
managedObjectContext: managedContext,
sectionNameKeyPath: "id", cacheName: nil)
do {
try fetchedDay.performFetch()
} catch {
fatalError("Failure to save context: \(error)")
}
我知道我可以得到这个ID:
var ID = fetchedID.sections![0].name
问题是:我也想同时获取每个id的性别。
我可以吗?
非常感谢!
最佳答案
使用“ id”作为sectionNameKeyPath
时,您将为每个唯一ID划分一个单独的部分,将具有相同ID的所有实例分为一个组。由于id可能是唯一的,这意味着每个组将是一个记录。与其为唯一字段分组,不如为sectionNameKeyPath
指定内容,通常最好仅指定nil
在每个部分中,您将获得完整记录,因此可以使用fetchedID.sections![n]
返回到nth
部分,并可以使用fetchedID.sections![x].objects![y]
获取单个记录以获取yth
部分中的xth
对象。 。
另外,您可以使用FetchedResultsController的fetchedObjects
属性来获取所有记录,而与节无关:fetchedID.fetchedObjects[n]
关于ios - 如何从NSFetchedResultsController获取其他部分的内容?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34870405/