I am getting this crash when I search data in my table view. I am using textfield as search bar. Here is my code:

NSMutableArray *searchArray;
NSString *searchTextString;
BOOL isFilter;
@property NSMutableArray *TableDataArray;


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.searchTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self updateSearchArray];
    [self.tableView reloadData];
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [searchArray count];
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"SubNotificationCell";
    SubNotificationCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    NSDictionary *dict;
    if(isFilter) {
        dict = [[self->searchArray objectAtIndex:indexPath.row] objectForKey:@"Eqktx"];
    } else {
       dict = [self.EtNotifRepTableDataArray objectAtIndex:indexPath.row];
    }

    NSString* notifValue = [dict objectForKey:@"Qmnum"];
    NSString* equipNameValue = [dict objectForKey:@"Eqktx"];
    NSString *values = @":";
    cell.notifValueLabel.text = [NSString stringWithFormat: @"%@ %@", values, notifValue];
    cell.equipNameValueLabel.text = [NSString stringWithFormat: @"%@ %@", values, equipNameValue];
    return cell;
}

-(void)textFieldDidChange:(UITextField*)textField {
    searchTextString = textField.text;
    [self updateSearchArray];
}

-(void)updateSearchArray {
    if (searchTextString.length != 0) {
        isFilter=YES;
        searchArray = [NSMutableArray array];
        for ( NSDictionary* item in _EtNotifRepTableDataArray ) {
            if ([[[item objectForKey:@"Eqktx"] lowercaseString] rangeOfString:[searchTextString lowercaseString]].location != NSNotFound) {
                [searchArray addObject:item];
            }
        }
    } else {
        isFilter=NO;
        searchArray = _EtNotifRepTableDataArray;
    }

    [self.tableView reloadData];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

I need to search for a number as well as for a string. But it's crashing with below error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x280e083f0'

in my viewdidload

@property NSMutableArray *EtNotifRepTableDataArray;

_EtNotifRepTableDataArray = [[NSMutableArray alloc]init];
    for (NSDictionary *dict in arr) {
        [dict description];
        NSString *Qmart = [dict objectForKey:@"Qmart"];
        NSString *Phase = [dict objectForKey:@"Phase"];
        if ([Qmart isEqualToString:_qmartValue] && [Phase isEqualToString:_phaseValue]){

            [self.EtNotifRepTableDataArray addObject:dict];
        }
    }

Related posts

Recent Viewed