【问题标题】:Retrieving objects from fetchedResultsController.sections?[section]从 fetchedResultsController.sections 检索对象?[section]
【发布时间】:2017-12-05 19:28:04
【问题描述】:

我正在使用以下内容填充 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";
})])

【问题讨论】:

    标签: swift core-data nsfetchedresultscontroller sections


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-11
      • 2011-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多