简体   繁体   中英

Searching NSMutableArray Crashing rangeOfString:options:]: unrecognized selector sent to instance

I am having a problem with the search facility in my app and i have noticed that some postings on here are saying that there is a problem with the for loop of my code which you can see at the link below from a previous posting.

Iphone Programming XML Parser NSMutableArray

This is the comment from another person with the same problem and i would just like to know if the problem is the same for my project, "The objects in your search array are NSArray objects, this means that they do not respond to the selector rangeOfString: as that is a NSString method" and if so can i use a solution like this one.

for(NSArray *array in searchArray)
  {
// NSString *str = [array objectAtIndex:0];
PatientInfoObject *obj = [array objectAtIndex:0];
NSString *str = obj.id;
// to be sure
if( [str isKindOfClass:[NSString class]] )
{
   NSRange titleResultsRange = [str rangeOfString:searchText options:NSCaseInsensitiveCompare];
   if( titleResultsRange.length != 0 )
   {
     [copyListOfItems addObject:str];
   }
}
else
{
    // this shouldn't have happened, log something to console
    NSLog(@"**Object in array is not of type NSString**");
 }
}

Here is the code for my UISearchBar, and as mentioned above the link at the beginning of the questions is to most of the relevant code if you need anything else i will post it.

Thanks Brad

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchtext {
if ([searchtext length] == 0) {
    [self toggleToolBarButtons:NO];
    [[self rssParser]startProcess];
}
else {
    for (NSString * string in _rssParser.rssItems) {
        NSRange r = [string rangeOfString:searchtext options:NSCaseInsensitiveSearch];
        if (r.location != NSNotFound) {
            [_rssParser.rssItems addObject:string];
        }
    }
}
[[self tableView]reloadData];
}
     for (NSString * string in _rssParser.rssItems) {

This code assumes that the the objects in the rssItems array are NSStrings. From the other post, it appears that they are instances of BlogRss.

Maybe you need something like:

 NSString *matchingStringToAdd=nil;
 for ( BlogRss * blogRss in _rssParser.rssItems ) {
      NSString *string = blogRss.title;  // or whatever member you're seaching on
      NSRange titleResultsRange = [string rangeOfString:searchText
                                                options:NSCaseInsensitiveCompare];
      if ( titleResultsRange.location != NSNotFound ) {
           matchingStringToAdd = string;
           break;
      }
 }
 if ( matchingStringToAdd ) {
     [rssParser.rssItems addObject:matchingStringToAdd);
 }


-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if ([searchText length] == 0) {
    //[self toggleToolBarButtons:NO];
    //[[self rssParser]startProcess];
    [displayitems removeAllObjects];
    [displayitems addObjectsFromArray:_rssParser.rssItems];
  } else {
    for ( BlogRss * blogRss in _rssParser.rssItems) {
        NSString *string = blogRss.title;  
        NSRange r = [string rangeOfString:searchText options:NSCaseInsensitiveSearch];
        if (r.location != NSNotFound) {
            [displayitems addObject:blogRss];
        }
    }
}
[[self tableView]reloadData];
}

rssfunviewcontroller.h

@interface RssFunViewController : UIViewController <UITableViewDataSource,UITableViewDelegate,BlogRssParserDelegate,UISearchBarDelegate> {
BlogRssParser * _rssParser;
UITableView * _tableView;
IBOutlet UISearchBar * searchbar;
RssFunAppDelegate * _appDelegate;
UIToolbar * _toolbar;
NSMutableArray * displayitems;
}

cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"rssItemCell"];
if(nil == cell){
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"rssItemCell"]autorelease];
}
cell.textLabel.text = [[displayitems objectAtIndex:indexPath.row]title];
cell.detailTextLabel.text = [[displayitems objectAtIndex:indexPath.row]description];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}

viewdidload

- (void)viewDidLoad {
[super viewDidLoad];
[self toolbarInit];
_rssParser = [[BlogRssParser alloc]init];
self.rssParser.delegate = self;
[[self rssParser]startProcess]; 
displayitems = _rssParser.rssItems;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM