开发者

TableView UISearchBar on Tab Bar Controller Crashes while Searching

开发者 https://www.devze.com 2023-01-24 23:45 出处:网络
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.

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 o开发者_如何转开发pen: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


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'.


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

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

To see which classes your dataArray contains.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号