0

My Problem Scenario is like this. I have an NSMutableArray ( Every Object is Nsstring). I have a UItextField ( as Client said) for Search.

I want know how to Search String into NSMutableArray like this

if I type A into textfield only those Content come from NSMutableArray which start From A. if I type AB into TextField only those Content Comes from NSMutableArray which is started from AB.. .... I am Trying NSRange Concept I like share Mycode

~

  for (int i=0; i<[[localTotalArrayForAwailable objectForKey:@"PUNCH"] count]; i++)
{

    NSString *drinkNamePuch= [[[localTotalArrayForAwailable objectForKey:@"PUNCH"]  objectAtIndex:i] drinkNames];

    NSRange titleResultsRange = [drinkNamePuch rangeOfString:searchText options:( NSCaseInsensitiveSearch)];

    if (titleResultsRange.length>0)
    {
        [searchArraypuch addObject:[[localTotalArrayForAwailable objectForKey:@"PUNCH"]  objectAtIndex:i]];
        [copyListOfItems setValue:searchArraypuch forKey:@"PUNCH"];
    }
}

~

Based on this code search not working proper as i need.

Thanks

4

4 回答 4

1

如果您试图searchText从一开始就找到与您匹配的所有字符串,那么您应该检查:

if ( titleresultsRange.location == 0 )

除此之外,我不确定什么是“无法正常工作”,您需要更好地解释您的预期结果是什么,以及您的实际结果是什么。

于 2012-10-31T19:58:13.463 回答
1

做这个;

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[cd] %@", searchText];
NSArray* filteredStrings = [[localTotalArrayForAwailable objectForKey:@"PUNCH"]  filteredArrayUsingPredicate:predicate];

在filteredStrings 中,你得到了所有以searchText 开头的字符串。

您可能会发现Predicate Programming Guide很有帮助。

于 2012-10-31T19:59:10.313 回答
0

您可以使用 NSArray/NSMutableArray 提供的这些方法:

  1. NSArray中,请参阅“在数组中查找对象”部分以了解以“indexesOfObjects...”开头的过滤方法,例如indexOfObjectsPassingTest:

  2. NSArray中,请参阅“Deriving New Arrays”部分以了解有关方法filteredArrayUsingPredicate 的内容:

  3. NSMutableArray中有一个方法filterUsingPredicate:

为了缩小结果范围,您可以继续将过滤连续应用于过滤后的数组或索引集。


indexOfObjectsPassingTest示例:使用

NSArray *strings = [NSArray arrayWithObjects:@"A", @"a", @"aB", @"AbC", @"Bag", @"Babc", @"baCK", @"", @"dba", nil];
NSString *searchString = @"Ab";

BOOL (^startsWithPredicate)(id, NSUInteger, BOOL*) = ^BOOL (id obj, NSUInteger idx, BOOL *stop) {
    NSString *string = (NSString *) obj;
    NSRange range = [string rangeOfString:searchString options:NSCaseInsensitiveSearch];
    return (range.location == 0);
};

NSIndexSet *indexSet = [strings indexesOfObjectsPassingTest:startsWithPredicate];
NSLog(@"Strings found: %@", [strings objectsAtIndexes:indexSet]);

输出:

Strings found: (
    aB,
    AbC
)
于 2012-10-31T20:11:52.647 回答
0

试试这个逻辑....它正在工作

 NSMutableArray *arr = [[NSMutableArray alloc]initWithObjects:@"aa",@"bbb",@"bb",@"cc",@"dd",@"ee",@"ff",@"gg",@"hh",@"ii", nil];
NSMutableArray *arrNew = [[NSMutableArray alloc]init];
NSString *strSearch = @"cccc";
int k = strSearch.length;
for (int i=0; i<[arr count]; i++) {
    for (int j=0; j<k; j++) {
        if (k<=[[arr objectAtIndex:i] length]) {
            if ([strSearch characterAtIndex:j] != [[arr objectAtIndex:i]characterAtIndex:j]) {
                break;
            }
            else if(j == k-1){
                [arrNew addObject:[arr objectAtIndex:i]];
            }
        }
    }
}
NSLog(@"%@",[arrNew description]);
于 2012-11-01T06:00:31.480 回答