Creating NSPredicate dynamically by setting the key programmatically

后端 未结 2 1275
不知归路
不知归路 2021-02-06 05:28

Why does the former of following snippets work while not the latter ?

Snippet 1

NSPredicate *predicate = [NSPredicate predicateWithForma         


        
相关标签:
2条回答
  • 2021-02-06 05:55

    I got the following error even though my NSPredicate was formatted correctly.

    *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Insufficient arguments for conversion characters specified in format string.' site:stackoverflow.com
    

    Like a fool I forgot to pass a second argument to the predicate format (because there were two %@). I.e. NSPredicate(format:predicateFormat,argumentArray:[Date()]) has only one element in the array when it needs to be two: [Date(), Date()]

    0 讨论(0)
  • 2021-02-06 05:56

    coin_unique is a key, so it needs the %K format specifier:

    NSString *predicateText = @"coin_unique";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%K == %@)", predicateText, [NSNumber numberWithInt:species]];
    

    The format syntax is described quite well here.

    0 讨论(0)
提交回复
热议问题