NSPredicate case-insensitive matching on to-many relationship

后端 未结 5 1545
攒了一身酷
攒了一身酷 2020-12-07 09:48

I am implementing a search field where the user can type in a string to filter the items displayed in a view. Each object being displayed has a keywords to-man

相关标签:
5条回答
  • 2020-12-07 10:17

    If you is trying to catch only the equals names but with insensitive case, I think it is the best solution

    [NSPredicate predicateWithFormat:@"ANY keywords.name LIKE[c] %@", ...];
    

    You helped me a lot. Thanks guys!!!

    In my case I did:

    [NSPredicate predicateWithFormat:@"ANY name LIKE[c] %@", @"teste"];
    
    0 讨论(0)
  • 2020-12-07 10:20

    If I understand you correctly, you want your predicate to be true whenever any keywords name matches the search string. For this you need to test with the ANY keyword like this:

    [NSPredicate predicateWithFormat:@"ANY keywords.name CONTAINS[c] %@", ...];
    

    This will search the keywords and return true if any of those keywords name contains your search string.

    0 讨论(0)
  • 2020-12-07 10:35

    If you want both case insensitive and wildcard, use this:

    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"(name like[c] '*%@*')",@"search"]];
    
    0 讨论(0)
  • 2020-12-07 10:38

    I believe the answer is:

    [NSPredicate predicateWithFormat:@"keywords.name CONTAINS[cd] %@", self.searchString];
    

    String comparisons are by default case and diacritic sensitive. You can modify an operator using the key characters c and d within square braces to specify case and diacritic insensitivity respectively, for example firstName BEGINSWITH[cd] $FIRST_NAME.

    Predicate Format String Syntax

    0 讨论(0)
  • 2020-12-07 10:39

    If you must match the keyword but the search must be case-insensitive then you should use NSPredicate(format: "keywords.name =[c] %@", self.searchString)

    LIKE does not work on string literals.

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