TableView UISearchBar on Tab Bar Controller Crashe

2019-08-20 19:43发布

问题:

I've been playing around with a search facility for my application table view for a while now trying to get it working but i keep getting the same error in my console.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ' [NSCFDictionary rangeOfString:options:]: unrecognized selector sent to instance

I believe that this following section may be the problem I have tried passing some NSLog entries inside the if statement and it seems to get through it but the problem is when I click on the search bar and starting typing, the first letter I type calls the error and cancels my app.

Here is where the problem is

In View Will Appear "Food" Array is initialized as below:

 NSString *myDBnew =@"/Users/taxsmart/Documents/rw3app.sql";

database = [[Sqlite alloc] init];

[database open:myDBnew];

NSString *quer = [NSString stringWithFormat:@"Select category from foodcat"];

Food = [database executeQuery:quer];

//[database executeNonQuery:quer];

[database close];

Search bar delegate method where error is encountered:

(void) searchTableView 

{

   NSString *searchText = searchBar.text;

   NSMutableArray *searchArray = [[NSMutableArray alloc] init];

//   [searchArray addObjectsFromArray:Food];

    for(NSDictionary *dictionary in Food)
    {
         NSString temp1 = [dictionary objectForKey:@"category"];
         [searchArray addObject:temp1];
    }

     for (NSString *sTemp in searchArray)

     {

              NSLog(@"Value: %@",NSStringFromClass([sTemp class]));

         NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

           if (titleResultsRange.length > 0)

               [copyListOfItems addObject:sTemp];
     }  

      [searchArray release];

       searchArray = nil;
}

What should I do?

Please Help.

Please Suggest

Thanks

回答1:

It looks that result of database query (Food) is dictionary that contains dictionary. This code:

for(NSDictionary *dictionary in Food)
{
     NSString temp1 = [dictionary objectForKey:@"category"];
     [searchArray addObject:temp1];
}

can be replaced with:

for(NSDictionary *dictionary in Food)
{
     NSObject *ob = [dictionary objectForKey:@"category"];
     if([ob isKindOfClass: [NSString class]]) 
     {
        [searchArray addObject:ob];
     } 
     else if([ob isKindOfClass: [NSDictionary class]])
     {
        NSDictonary *dic1 = (NSDictionary*)ob;
        // ... at this point you can get the string for desired dictionary key 
        // or you can ignore it
     }
}

With this code we can be sure that only strings are put into searchArray.

If you want to make full tree parsing for desired key 'category' then you should make some recursive function to search the dictionary.

You can dump Food variable to console to see at which leaf is actually the result you are looking for. Put the break-point and into console type 'po Food'.



回答2:

Appears that there is an NSDictionary in your dataArray. Add an

NSLog(@"%@",NSStringFromClass([description class]])); 

To see which classes your dataArray contains.