5

我有一個UITableViewCore Data提供,NSFetchedResultsController返回位置實體。默認排序(和節標題)是通過實體名稱的第一個字母。這有效(雖然我仍然試圖將大寫和小寫合併到同一部分中)。用戶可以選擇通過三個可選類別(這是實體的屬性)中的一個來排序表,然後對這些類別進行排序按實體名稱。使用NSFetchedResultsController排序描述符 - 斯威夫特

當我設置爲按類別排序,我得到以下運行時錯誤:

[_TtCSs23_ContiguousArrayStorage00007F80513B59D0 key]: unrecognized selector sent to instance 0x7f80513b5720 

這是我NSFetchedResultsController

var sectionNameKeyPathString1: String? 
var sectionNameKeyPathString2: String? 

var fetchedResultsController: NSFetchedResultsController { 
    if _fetchedResultsController != nil { 
     return _fetchedResultsController! 
    } 

    let fetchRequest = NSFetchRequest() 
    // Edit the entity name as appropriate. 
    let entity = NSEntityDescription.entityForName("Location", inManagedObjectContext: self.managedObjectContext!) 
    fetchRequest.entity = entity 

    // Set the batch size to a suitable number. 
    fetchRequest.fetchBatchSize = 20 

    // Edit the sort key as appropriate. 
    if sectionNameKeyPathString1 != nil { 
     let sortDescriptor1 = NSSortDescriptor(key: sectionNameKeyPathString1!, ascending: true) 
     let sortDescriptor2 = NSSortDescriptor(key: sectionNameKeyPathString2!, ascending: true) 
     let sortDescriptors = [sortDescriptor1, sortDescriptor2] 
     fetchRequest.sortDescriptors = [sortDescriptors] 
    } else { 
     let sortDescriptor = NSSortDescriptor(key: "locationName", ascending: true) 
     fetchRequest.sortDescriptors = [sortDescriptor] 
    } 

    var sectionNameKeyPath: String 
    if sectionNameKeyPathString1 == nil { 
     sectionNameKeyPath = "firstLetterAsCap" 
    } else { 
     sectionNameKeyPath = sectionNameKeyPathString1! 
    } 

    // Edit the section name key path and cache name if appropriate. 
    // nil for section name key path means "no sections". 
    let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: sectionNameKeyPath, cacheName: "Locations") 
    aFetchedResultsController.delegate = self 
    _fetchedResultsController = aFetchedResultsController 

    var error: NSError? = nil 
    if !_fetchedResultsController!.performFetch(&error) { 
     // TODO: Handle this error 
     // Replace this implementation with code to handle the error appropriately. 
     // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
     //println("Unresolved error \(error), \(error.userInfo)") 
     abort() 
    } 

    return _fetchedResultsController! 
} 

使用斷點我有信心,例如,sectionNameKeyPathString1 =「 category1「和sectionNameKeyPathString2 =」locationName「以及sectionNameKeyPath =」category1「,因此鍵路徑與第一個排序描述符匹配。

我曾在Obj-C工作過,但現在我正在拉我的頭髮,確定我患有盲目性。

回答

10

難道你只是有太多的[]?

let sortDescriptors = [sortDescriptor1, sortDescriptor2] // <- an [NSSortDescriptor] 
    fetchRequest.sortDescriptors = [sortDescriptors] // <- now an [[NSSortDescriptor]] 

應該僅僅是:

fetchRequest.sortDescriptors = [sortDescriptor1, sortDescriptor2] 
+0

你回答我的問題,而我還在編輯。謝謝!在時間限制結束後我會接受你的回答。我很高興人們盯着我看。 – Magnas 2014-09-05 10:45:01

+0

太奇怪了,我也一樣!由於我初始化了數組初始化符中的描述符,並且太習慣於將這些方法放在ObjC的方括號中,所以必須有雙敲方括號。 – Ash 2014-10-28 07:28:25