【问题标题】:NSPredicate: Unable to parse the format string for relationshipNSPredicate:无法解析关系的格式字符串
【发布时间】:2016-08-03 06:30:56
【问题描述】:

我有两个名为“CIUser”和“CICast”的实体。 “CIUser”实体与名为“cast”的“CICast”具有一对一的关系。

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 &lt;= %@"'

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

【问题讨论】:

    标签: swift core-data nspredicate entity-relationship one-to-one


    【解决方案1】:

    问题在于“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 在“谓词编程指南”中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多