核心数据获取请求错误

时间:2017-09-28 13:39:28

标签: ios iphone swift xcode core-data

我正在尝试根据属性名称获取记录。

我也尝试搜索类似的问题,但我无法找到解决方案,所以我发帖问题......

但我在获取时遇到异常:

[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 个答案:

答案 0 :(得分:1)

您正在错误地构建谓词字符串。要匹配的值必须在引号内,因此您希望像这样构建它:

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

哪个解析为以下谓词字符串:

imagePath LIKE[c] "audio1.m4a"

请注意您匹配的值周围的引号,以及属性名称周围缺少引号。您当前的谓词正在尝试查找其imagePath属性与其audio1.m4a属性具有相同值的记录,但该记录无效。

相关问题