Form NSPredicate from string that contains id's

前端 未结 1 1498
面向向阳花
面向向阳花 2020-12-11 11:33

I have this problem where I can\'t think of how to write this predicate.

I have an Entity called Contact, it has a string property \"pages\", let\'s say



        
相关标签:
1条回答
  • 2020-12-11 12:07

    The "MATCHES" operator of NSPredicate can be used to compare against a regular expression:

    NSString *pageId = @"1";
    NSString *regex = [NSString stringWithFormat:@"(.*,)?%@(,.*)?", pageId];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"pages MATCHES %@", regex];
    

    But you should also consider to replace the string property by a to-many relationship to a Page entity, then the predicate would look like

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY pages.pageid == %@", pageId];
    
    0 讨论(0)
提交回复
热议问题