从fetchedResultsController.sections中检索对象?[section]

时间:2017-12-05 19:28:05

标签: swift core-data nsfetchedresultscontroller sections

我使用以下内容填充UITableView部分的部分:

fetchedResultsController =
  NSFetchedResultsController(fetchRequest: fetchRequest,
                             managedObjectContext: managedContext,
                             sectionNameKeyPath: #keyPath(Players.team),
                             cacheName: nil)

我的UITableView在该部分中有team。我还想将城市添加到该部分。我不认为我可以在该部分添加两个项目。我可以看到以下内容包含所需的数据。但是如何检索城市信息?

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    let sectionInfo = fetchedResultsController.sections?[section]

    print("sectionInfo \(String(describing: sectionInfo?.objects))")

    return sectionInfo?.name
}

结果:

sectionInfo Optional([<Time_on_Ice.Players: 0x1c028cf30> (entity: Players; id: 0xd000000000100000 <x-coredata://EE1C8D6B-3FC5-4460-9F7F-3830328B6688/Players/p4> ; data: {
    birthdate = "1990-02-07 05:00:00 +0000";
    city = "Tampa Bay";
    division = Atlantic;
    firstName = Steven;
    headshot = <ffd8ffe0 00104a46 49460001 01000048 00480000 ffe10058 45786966 00004d4d 002a0000 00080002 01120003 00000001 0001>;
    height = "5'11\"";
    lastName = Stamkos;
    league = NHL;
    level = Professional;
    number = 91;
    playersShiftRelationship = "<relationship fault: 0x1c403d060 'playersShiftRelationship'>";
    position = Centre;
    shoots = Right;
    team = Lightning;
    weight = "177 lb";
})])

1 个答案:

答案 0 :(得分:0)

看起来你没有团队实体,每个玩家都独立包含所有团队信息。这种非规范化在核心数据中并不一定是错误的。因此,如果每个拥有相同团队的实体都拥有相同的城市,您只需访问部分中的第一个元素并获取它的city属性即可。对于

部分中的每个元素,它都是相同的