7

我有一组自定义对象。自定义对象如下所示

@interface User : NSObject
@property(nonatomic, strong)NSString *user_Id;
@property(nonatomic, strong)NSString *user_Name;
@property(nonatomic, strong)NSString *user_UserName;
@end

我必须过滤检查 2 个属性的数组。也就是说,如果我搜索a,那么它应该得到从数组中过滤的用户列表,包含auser_Name或中。user_Id我怎样才能做到这一点?对于我知道的单个属性[user_Name]

NSString *predicateString = @"user_Name MATCHES[c] %@";
NSString *matchString =  [NSString stringWithFormat: @".*%@.*",searchText];
NSPredicate *predicate =[NSPredicate predicateWithFormat:predicateString, matchString];
self.searchResults = [userArray filteredArrayUsingPredicate:predicate];
4

3 回答 3

3

You can join predicate conditions with OR, such as:

NSString *predicateString = @"(user_Name MATCHES[c] %@) OR (user_Id MATCHES[c] %@)";

Alternately, you could filter the array by using indexesOfObjectsPassingTest: with an appropriate test block and then objectsAtIndexes: to get an array of the objects passing the test.

于 2013-03-23T04:06:31.123 回答
1

尝试使用这个谓词字符串

NSString *predicateString = @"user_Name MATCHES[c] %@ OR user_Id MATCHES[c] %@";
于 2013-03-23T04:07:08.463 回答
1
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(user_Name== %@) || (user_Id== %@), <name>,  <id>];
于 2013-03-23T04:06:49.807 回答