2017-09-28 139 views
0

我想基於屬性名稱獲取記錄。核心數據獲取請求錯誤

我也試圖尋找類似的問題,但我不能夠找到解決辦法,所以我張貼問題...

,但我得到的異常而獲取:

[error] error: exception handling request: <NSSQLFetchRequestContext: 0x1c019a270> , keypath audio_1.m4a not found in entity <NSSQLEntity answer id=8> with userInfo of (null) 
CoreData: error: exception handling request: <NSSQLFetchRequestContext: 0x1c019a270> , keypath audio_1.m4a not found in entity <NSSQLEntity answer id=8> with userInfo of (null) 
2017-09-28 19:03:36.725060+0530 app[9684:891158] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath audio_1.m4a not found in entity <NSSQLEntity answer id=8>' 

我的代碼:

let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: Entity) 

    if let predic = predicate_str{ 
     let predicate = NSPredicate(format: predic) 
     fetchRequest.predicate=predicate 
    } 

    do { 
     let arr = try appsession.managedObjectContext.fetch(fetchRequest) //CRASHED 


     // success ... 
    } catch let error as NSError { 
     // failure 
     print("Fetch failed: \(error.localizedDescription)") 
     return(nil,error) 
    } 

謝謝

+1

什麼是'predicate_str'?什麼是「實體」? – jrturton

+0

@jrturton,實體是「答案」&predicate_str =「imagePath LIKE [c] audio_1.m4a」 – nirav

回答

1

您正在錯誤地構建謂詞字符串。值,以匹配必須引號內,所以你想建立這樣的:

let predicate = NSPredicate(format: "%K LIKE[c] %@", "imagePath", "audio1.m4a") 

解析爲以下謂詞字符串:

imagePath LIKE[c] "audio1.m4a" 

注意周圍的值,你」引號重新匹配,並且缺少屬性名稱周圍的引號。您當前的謂詞試圖查找其imagePath屬性與audio1.m4a屬性具有相同值的記錄,該屬性不起作用。

+0

謝謝,它的工作原理! – nirav