0

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";
    },
6
  • 1
    What is the crash? Also, this is not all the code - what is 'counter', what is Array? Commented Feb 27, 2014 at 7:59
  • We need crash details. But you might want to look at using a predicate instead of a loop. Commented Feb 27, 2014 at 8:01
  • does all the elements in arr is NSString, seems like element in arr are NSDictionary. Commented Feb 27, 2014 at 8:03
  • 1
    That error means that arr contains NSDictionaries and not NSStrings. Commented Feb 27, 2014 at 8:03
  • My arr contains value in this format { "Merchant_Id" = 1012; "Merchant_Name" = AajKiItem; "Merchant_Url" = aajkiitem; }, { "Merchant_Id" = 1013; "Merchant_Name" = Aaneri; "Merchant_Url" = aaneri; }, { "Merchant_Id" = 1015; "Merchant_Name" = Adexmart; "Merchant_Url" = adexmart; }, ... I want to compare on Merchant_Name how should i compare ? Commented Feb 27, 2014 at 8:13

3 Answers 3

1

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];
Sign up to request clarification or add additional context in comments.

2 Comments

"NSPredicate … It's much faster and much more optimised then what you could write." - Do you have any reference confirming your statement? In my experience, filteredArrayUsingPredicate is slower than an explicit loop, compare stackoverflow.com/a/21158730/1187415.
It crashes here NSArray *beginWithB = [arr filteredArrayUsingPredicate:bPredicate]; shows Can't do a substring operation with something that isn't a string (lhs = { "Merchant_Id" = 1007; "Merchant_Name" = 24HoursLoot; "Merchant_Url" = 24hoursloot; } rhs = a)
1

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;
}

4 Comments

Tried your answer and shows crash here NSArray *filter = [arr filteredArrayUsingPredicate:predicate];and my log shows this Can't do a substring operation with something that isn't a string (lhs = { "Merchant_Id" = 1007; "Merchant_Name" = 24HoursLoot; "Merchant_Url" = 24hoursloot; } rhs = a)
Or i tried your first answer i am not getting value of searchText log shows : Merchant_Name LIKE[cd] "%@" and my array filter is null
do me a faviour send your arr result to me.
See my EDIT in the question i want only Merchant_Name
0

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++;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.