How to join two strings for NSPredicate, ie firstname and lastname

后端 未结 3 2128
野趣味
野趣味 2021-02-19 09:29

I have a Person Object which has two NSString properties; firstName and lastName. I\'m currently using an NSPredicate like so

3条回答
  •  甜味超标
    2021-02-19 10:11

    you can concatenate the fields into two common fields (firstLastName and lastFirstName )

    - (NSString *)firstLastName {
          return [NSString stringWithFormat:@"%@ %@", self.firstName, self.lastName];
    }
    
    - (NSString *)lastFirstName {
          return [NSString stringWithFormat:@"%@ %@", self.lastName, self.firstName];
    }
    

    and then filter on this fields using 'contains[cd]'

    [NSPredicate predicateWithFormat:@"(firstLastName contains[cd] %@) OR (lastFirstName contains[cd] %@)" , self.searchBar.text, self.searchBar.text];
    

提交回复
热议问题