1

I am developing iOS app, in which i have searchBar.

What i want is,

when user types s in the searchBar, i want to get all the Merchant_Name who starting with letter s from my array

Here is my code snippet i have tried but it is not working,

My arr contains:

 arr at index 0 = {
        "Merchant_Id" = 1261;
        "Merchant_Name" = "Magazine Mall";
        "Merchant_Url" = "magazine-mall";
    },
     arr at index 1 =   {
        "Merchant_Id" = 1262;
        "Merchant_Name" = MakeMyTrip;
        "Merchant_Url" = makemytrip;
    },
   arr at index 2 =     {
        "Merchant_Id" = 1263;
        "Merchant_Name" = Travelguru;
        "Merchant_Url" = travelguru;
    },
    arr at index 3 =    {
        "Merchant_Id" = 1266;
        "Merchant_Name" = Travelchacha;
        "Merchant_Url" = travelchacha;
    },
      arr at index 4 =  {
        "Merchant_Id" = 1267;
        "Merchant_Name" = Tradus;
        "Merchant_Url" = tradus;
    },
    arr at index 5 =    {
        "Merchant_Id" = 1268;
        "Merchant_Name" = ToyWorld;
        "Merchant_Url" = toyworld;
    },
     arr at index 6 =   {
        "Merchant_Id" = 1269;
        "Merchant_Name" = Topskin;
        "Merchant_Url" = topskin;
    },
   ....

Code:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Merchant_Name LIKE[cd] %@ ",searchText];
NSMutableArray *filter1 =[[NSMutableArray alloc] init];
filter1 = [[arr filteredArrayUsingPredicate:predicate] mutableCopy];
NSLog(@"predicate  = %@ -------  filter=%@",predicate,filter1);

Log Shows:

predicate  = Merchant_Name LIKE[cd] "s" -------  filter=(
)

Please help me, Where i am doing mistake?

Thanks in advance.

codercat
  • 22,873
  • 9
  • 61
  • 85
Krunal
  • 6,440
  • 21
  • 91
  • 155
  • http://stackoverflow.com/questions/9509138/ios-sorting-array-of-dictionaries-by-key-of-inner-dictionary-and-showing-respect – codercat Mar 04 '14 at 07:51

3 Answers3

2
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"name beginswith[c] %@", searchText];

NSMutableArray *filter1 =[[NSMutableArray alloc] init];
filter1 = [[arr filteredArrayUsingPredicate:predicate] mutableCopy];
NSLog(@"predicate  = %@ -------  filter=%@",predicate,filter1);

more understand follow this

https://developer.apple.com/library/mac/documentation/cocoa/conceptual/predicates/Articles/pUsing.html

codercat
  • 22,873
  • 9
  • 61
  • 85
1

Change your LIKE for BeginsWith

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Merchant_Name beginswith[c] %@ ",searchText];
NSMutableArray *filter1 =[[NSMutableArray alloc] init];
filter1 = [[arr filteredArrayUsingPredicate:predicate] mutableCopy];
NSLog(@"predicate  = %@ -------  filter=%@",predicate,filter1);
Fran Martin
  • 2,369
  • 22
  • 19
0
[NSPredicate predicateWithFormat:@"Merchant_Name beginswith %@", [searchText uppercaseString]];
Sunny Shah
  • 12,990
  • 9
  • 50
  • 86