NSPredicate: Combine CONTAINS with IN

后端 未结 4 831
庸人自扰
庸人自扰 2020-11-29 09:42

I have a set of users in CoreData and an search field in my application. User has the properties firstname and name.

Currently I have a predicate like \"user.name CO

4条回答
  •  眼角桃花
    2020-11-29 10:18

    I had the same problem and solved it like this:

    In my NSManagedObject class (user) I added the following method, merging the two values into a single string:

    - (NSString *)name
    {
        return [NSString stringWithFormat:@"%@ %@", self.firstname, self.lastname];
    }
    

    Then you'll only need the following lines to match against your list, which is in my case an NSArray with user objects:

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchString];
    NSArray *filteredUsers = [users filteredArrayUsingPredicate:predicate];
    

提交回复
热议问题