Krunal
Krunal

Reputation: 6490

Getting a values from array

I have searchBar in my app,

What i want to do is when user types any character in searchBar, I want to get all the values starting with same character from my array.

Here is my code snippet i have tried but it crashes..

for (int i=0; i<[arr count]; i++) {
    for(NSString *name in [[arr objectAtIndex:i] objectForKey:@"Merchant_Name"])
    {

        NSRange r = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];

        if(r.location != NSNotFound)
        {
            if(r.location== 0)//that is we are checking only the start of the naames.
            {
                [arr addObject:name];

            }
        }

    }
}

Shows ERROR:-[__NSDictionaryM rangeOfString:options:]: unrecognized selector sent to instance

Where i am making mistake?

Please help and thanks in advance.

EDIT:

arr =   {
        "Merchant_Id" = 1036;
        "Merchant_Name" = "Arzoo Hotels";
        "Merchant_Url" = "arzoo-hotels";
    },
        {
        "Merchant_Id" = 1037;
        "Merchant_Name" = "Ashika Mall";
        "Merchant_Url" = "ashika-mall";
    },
        {
        "Merchant_Id" = 1038;
        "Merchant_Name" = "At My Doorsteps";
        "Merchant_Url" = "at-my-doorsteps";
    },
        {
        "Merchant_Id" = 1039;
        "Merchant_Name" = "AVA Host";
        "Merchant_Url" = "ava-host";
    },

Upvotes: 0

Views: 192

Answers (3)

utkal patel
utkal patel

Reputation: 1421

Using Below code you can do straight search

  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Merchant_Name LIKE[cd] '%@' ",searchText];
  NSArray *filter = [arr filteredArrayUsingPredicate:predicate];

Using Below code you can do custom search

  NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Merchant_Name  beginswith[c] 'a'"];
  NSArray *aNames = [arr filteredArrayUsingPredicate:predicate]

-use this code -And Set Delegate to Uitextfield.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSString *changedText = [textField.text stringByReplacingCharactersInRange:range withString:string];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF['Merchant_Name'] contains %@",changedText];
    NSArray *filter = [arraydata filteredArrayUsingPredicate:predicate];

    NSLog(@"%@%",changedText);

    return YES;
}

Upvotes: 1

simalone
simalone

Reputation: 2768

There is NSMutableDictionary type object in arr, try this to avoid crash:

 for(NSString *name in arr){
    if(![name isKindOfClass:[NSString class]]){
        continue;
    }

    NSRange r = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];
    if(r.location != NSNotFound)
    {
        if(r.location== 0)//that is we are checking only the start of the naames.
        {
            [Array addObject:name];
        }
    }
    counter++;
}

Upvotes: 0

Lord Zsolt
Lord Zsolt

Reputation: 6557

For that, you should filter your array using NSPredicate.

Edit: Using predicate and filtering is much more readable and understandable. As stated in the comment, it might be slower, but you probably won't have 10,000,000+ strings you'd like to filter through, so saving 0.00001 second probably isn't worth writing a code 10 lines longer.

Documentation here.

And here's a whole tutorial about search bar and filtering data.

A quick example to filter an array and get values that begin with 'a' or 'c':

NSMutableArray *array =
    [NSMutableArray arrayWithObjects:@"Nick", @"Ben", @"Adam", @"Melissa", nil];

NSPredicate *bPredicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] 'a'"];
NSArray *beginWithB = [array filteredArrayUsingPredicate:bPredicate];
// beginWithB contains { @"Adam" }.

NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] 'e'"];
[array filterUsingPredicate:sPredicate];

Upvotes: 1

Related Questions