2017-09-28 49 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