Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
795 views
in Technique[技术] by (71.8m points)

iphone - TableView UISearchBar on Tab Bar Controller Crashes while Searching

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...