NSPredicate with dynamic key and value

前端 未结 2 750
心在旅途
心在旅途 2021-01-17 17:20

I am trying to create a function that searches through my core data stack and return the amount of messages for a desired key/value.

Here is my code:



        
相关标签:
2条回答
  • 2021-01-17 17:43
     let predicate = NSPredicate(format: "SELF[%@] CONTAINS %@",key,value)
    

    This worked for me

    0 讨论(0)
  • 2021-01-17 18:03

    To substitute a key in the predicate string, you have to use %K, not %@:

    [NSPredicate predicateWithFormat:@"%K == %@", key, value];
    

    From the Predicate Format String Syntax documentation:

    • %@ is a var arg substitution for an object value—often a string, number, or date.
    • %K is a var arg substitution for a key path.
    0 讨论(0)
提交回复
热议问题