2016-08-03 38 views
1

我有两个实体名为“CIUser”和“CICast”。 “CIUser”实体与名为“cast”的“CICast”具有一对一关系。NSPredicate:无法解析关系的格式字符串

CIUser : 
    -> userId(Int) 
    -> isLive(bool) 
    -> name(String) 

CICast: 
    -> castId(Int) 
    -> lastUpdate(Date) 

现在我的要求是获取所有当前正在运行的用户,lastUpdate小于计算日期。所以我准备了我喜欢

let time = //an calculated NSDate object 

let fetchRequest = NSFetchRequest(entityName: "CIUser") 
fetchRequest.predicate = NSPredicate(format: "isLive == %@ AND cast.lastUpdate <= %@", NSNumber(bool: true), time) 

取请求,但它应用程序崩溃扔*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "isLive == %@ AND cast.lastUpdate <= %@"'

谁能帮我哪里做错了,或者我应该采取什么办法。建议将不胜感激。

回答

3

问题在于“CAST”是保留字的谓词格式 语法,保留字不区分大小写。所以这与您名为“cast”的关系冲突 。

作为一种变通方法,可以使用%K关键路径替换:

fetchRequest.predicate = NSPredicate(format: "isLive == %@ AND %K <= %@", 
          NSNumber(bool: true), "cast.lastUpdate", time) 

或重命名的关系。您可能需要使用%K扩张 一般避免这样的问题:

fetchRequest.predicate = NSPredicate(format: "%K == %@ AND %K <= %@", 
          "isLive", NSNumber(bool: true), 
          "cast.lastUpdate", time) 

欲了解更多信息,请参阅Predicate Format String Syntax 在“谓词编程指南”。

+0

感谢@Martin,它的功能就像一个魅力。 –