本文介绍了在ios中过滤数组,检查多个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组自定义对象。自定义对象看起来像这样

I have an array of custom objects. The custom object look like this

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

我必须过滤数组检查2个属性。那就是我搜索 a 然后它应该从 user_Name user_Name a c $ c>或 user_Id 。我如何实现这一目标?对于单个属性,我知道[user_Name]

I have to filter the array checking 2 properties.That is if I search a then it should get list of users filtered from array contains a in user_Name or user_Id .How can i achieve this? For a single property i know[user_Name]

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


推荐答案

,例如:

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

或者,您可以使用 indexesOfObjectsPassingTest过滤数组:使用适当的测试块然后 objectsAtIndexes:获取通过测试的对象数组。

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.

这篇关于在ios中过滤数组,检查多个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 03:50