5

在以下代码中:

NSFetchedResultsController *frc =
[[NSFetchedResultsController alloc]
 initWithFetchRequest:fetchRequest
 managedObjectContext:myManagedObjectContext
 sectionNameKeyPath:@"store"
 cacheName:@"SomeCache"
 ];

sectionNameKeyPath 的 @"store" 值实际上指向一个 Store 实体的关系,该实体具有我真正需要的名称属性作为我的部分标题标题。

但是我的代码设置方式无法完成,因为我得到的部分标题如下: 0x5b504f0 0x5b51190 据我所知,这是正在获取的 Store 对象的代码数据地址。

我如何使用 NSFetchedResultsController 以便我可以告诉它我希望它从 sectionNameKeyPath:@"store" 获取它获取的任何内容的名称属性?还是有其他解决方法?

4

2 回答 2

5

尝试sectionNameKeyPath:@"store.name"

于 2011-08-31T00:14:39.683 回答
1

我知道这是一个旧线程,但我想使用 Swift 添加我的解决方案。

我必须建议这个解决方案取决于返回的部分名称的格式NSFetchedResultsController,因此它取决于 Apple 可以更改的内部实现。

部分名称包含持久数据存储中对象 ID 的 URI,因此您可以通过先提取 URI,然后向 Store 请求具有相应 URI 的对象来获取对象

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    let sectionInfo = fetchedResultsController?.sections![section]
    let sectionName = sectionInfo?.name

    //This part depends on the internal implementation of sectionInfo.name
    let initialRange = sectionName?.rangeOfString("<")
    let finalRange = sectionName?.rangeOfString(">")

    let uri = sectionName?.substringWithRange((initialRange?.endIndex)!..<(finalRange?.startIndex)!)
    let url = NSURL(string: uri!)

    let store = fetchedResultsController?.managedObjectContext.persistentStoreCoordinator

    let objectID = store?.managedObjectIDForURIRepresentation(url!)
    let coreObject = fetchedResultsController?.managedObjectContext.objectWithID(objectID!)
    //return the correct property from the object as the title
    let title = ...
    return title
}

好吧,你需要检查错误(我会guard在前面使用很多let),但你明白了。

于 2016-07-11T11:58:57.510 回答