简体   繁体   English

如果有匹配项,如何在数组上执行搜索,然后将这些项目复制到ipad应用程序中的另一个数组中

[英]How to perform search on array if it has items matching then copy those items in another array in ipad app

I have a app in which i want searching. 我有一个要搜索的应用程序。 I have a array resultArray which contain all the things which display like 我有一个数组resultArray,其中包含显示的所有内容

 Book*bookObj=[resultArray objectAtIndex.indexPath.row];
 NSString*test=bookObj.title;

I want to perform search on title item in resultArray if search text enter in textfield matches with title with any of the arrays then copy those all array values in testArray. 我想对resultArray中的标题项执行搜索,如果在文本字段中输入的搜索文本与具有任何数组的title匹配,然后将这些所有数组值复制到testArray中。

- (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate is exactly the function that you want. -(NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate正是您想要的功能。 It will return a new array with only the elements that pass the test in the NSPredicate object. 它将返回一个仅包含通过NSPredicate对象中的测试的元素的新数组。

For example: 例如:

NSArray *newArray = [oldArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(Book *evaluatedObject, NSDictionary *bindings) {
    //Do whatever logic you want to in here
    return [evaluatedObject.title isEqualToString:theTitle];
}];

You have to take another array for this. 您必须为此采取另一个数组。 this will add your object 这将添加您的对象

for (Book * bookObj in resultArray) {
         NSString *strName=[[bookObj.title]lowercaseString];
         if ([strName rangeOfString:searchText].location !=NSNotFound) {
                [arrTemp addObject:bookObj];
            }
        }

Use this as : 将此用作:

   NSMutableArray *searchDataMA = [NSMutableArray new];

       for (int i = 0; i < resultArray.count; i++) {

         Book *bookObj=[resultArray objectAtIndex:i];
         NSString*test=bookObj.title;
        NSRange rangeValue1 = [test rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if (rangeValue1.length != 0) {

            if (![resultArray containsObject:test]) {
                [searchDataMA addObject:test];
            }

        }
    }

It works for me try this: 它对我有用,试试这个:

NSArray *fruits = [NSArray arrayWithObjects:@"Apple", @"Crabapple", @"Watermelon", @"Lemon", @"Raspberry", @"Rockmelon", @"Orange", @"Lime", @"Grape", @"Kiwifruit", @"Bitter Orange", @"Manderin", nil];
NSPredicate *findMelons = [NSPredicate predicateWithFormat:@"SELF contains[cd] 'melon'"];
NSArray *melons = [fruits filteredArrayUsingPredicate:findMelons];
NSPredicate *findApple = [NSPredicate predicateWithFormat:@"SELF beginswith 'Apple'"];
NSArray *apples = [fruits filteredArrayUsingPredicate:findApple];
NSPredicate *findRNotMelons = [NSPredicate predicateWithFormat:@"SELF beginswith 'R' AND NOT SELF contains[cd] 'melon'"];
NSArray *rNotMelons = [fruits filteredArrayUsingPredicate:findRNotMelons];

NSLog(@"Fruits: %@", fruits);
NSLog(@"Melons: %@", melons);
NSLog(@"Apples: %@", apples);
NSLog(@"RNotMelons: %@", rNotMelons);

Predicates also have more condition functions, some of which I have only touched on here: 谓词还具有更多的条件函数,我在这里仅涉及其中一些:

beginswith : matches anything that begins with the supplied condition
contains : matches anything that contains the supplied condition
endswith : the opposite of begins with
like : the wildcard condition, similar to its SQL counterpart. Matches anything that fits the wildcard condition
matches : a regular expression matching condition. Beware: quite intense to run

The syntax also contains the following other function, predicates and operations: 该语法还包含以下其他函数,谓词和操作:

AND (&&), OR (||), NOT (!)
ANY, ALL, NONE, IN
FALSE, TRUE, NULL, SELF

Still if don't understand take a look at this link; 仍然,如果不理解,请查看此链接;

Useful link 有用的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM