2017-03-07 182 views
2

我試圖在NSPredicate中使用IN子句。我收到以下錯誤:Swift核心數據謂詞IN子句

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0xa000000000000611'

下面的代碼:

let fetchRequest: NSFetchRequest<Employee> = NSFetchRequest(entityName: "Employee") 

fetchRequest.sortDescriptors = [ 
    NSSortDescriptor.init(key: "lastName", ascending: true) 
] 

fetchRequest.predicate = NSPredicate(format: "ANY id IN %@", argumentArray: recentEmployeeIds) 

fetchedResultsController = NSFetchedResultsController.init(fetchRequest: fetchRequest, 
                      managedObjectContext: FLCoreDataController.shared.mainObjectContext, 
                      sectionNameKeyPath: nil, 
                      cacheName: nil) 
fetchedResultsController?.delegate = self 

try? fetchedResultsController?.performFetch() 

任何想法的問題是什麼?

+2

刪除'argumentArray無標籤:'標籤。你只傳遞一個參數給你的謂詞(這是一個數組),而不是一個參數數組。 – dan

+0

這工作!非常感謝你的幫助。 –

回答

3

你不告訴你是如何定義recentEmployeeIds,但假設它像

let recentEmployeeIds:[Int] = ... 

,那麼你需要正確地初始化NSPredicate。沒有爲argumentArray

fetchRequest.predicate = NSPredicate(format: "ANY id IN %@", recentEmployeeIds) 
+0

刪除論點是解決方案。非常感謝! –

+0

@BryanDeemer你能標記我接受的答案嗎? – dmorrow