簡體   English   中英

使用NSFetchedResultsController和sectionNameKeyPath是關系時如何獲取節名稱?

[英]How to fetch section name when using NSFetchedResultsController and sectionNameKeyPath is a relationship?

在下面的代碼中:

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

sectionNameKeyPath的@“ store”值實際上指向與Store實體的關系,該實體具有我確實需要的name屬性作為節標題的標題。

但是我的代碼設置方式無法實現,因為我改為獲得以下部分標題:0x5b504f0 0x5b51190這些是我所知的要獲取的Store對象的代碼數據地址。

我如何使用NSFetchedResultsController以便告訴我我希望它獲取從sectionNameKeyPath:@“ store”獲取的內容的name屬性? 還是有其他解決方法?

嘗試sectionNameKeyPath:@"store.name"

我知道這是一個舊線程,但是我想使用Swift添加我的解決方案。

我必須建議該解決方案取決於NSFetchedResultsController返回的節名稱的格式,因此它取決於Apple可以更改的內部實現。

部分名稱包含Persistent Data Store中對象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
}

好吧,您需要檢查錯誤(我會在let之前使用很多guard ),但是您明白了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM