0
votes

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];
        }
    }
1
Please show how is _EtNotifRepTableDataArray defined and filledAndreas Oetjen
i am using sqlite to save the data. Please check my post. I hv updateddavid
Seems arr here for (NSDictionary *dict in arr) contains NSString object instead of NSDictionary. Can you add the log of arr in your question?TheTiger
[__NSCFString objectForKey:] means you are calling objectForKey for a NSString object. Eventhough you stated NSDictionary "dict" but somehow "dict" holds a nsstring in one of your codes hence it causes this error. You need to NSLog step by step to debug this to see where are you not getting a dictionary.GeneCode

1 Answers

1
votes

You replace your cellForRowAtIndexPath method with this.

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

    NSDictionary *dict;
    if(isFilter) {
        // HERE was the problem. 
        //dict = [[self->searchArray objectAtIndex:indexPath.row] objectForKey:@"Eqktx"];
        dict = [self.searchArray objectAtIndex:indexPath.row]
    } 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;
}

The problem is that you are fetching the object directly in filter part of cellForRowAtIndexPath method which returning you a NSString Value, then again in you are trying to get the objectForKey from this NSString which leads to crash, because NSString has no known method objectForKey

EDIT:

It's not related to the crash but you should also update this method based on filter.

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(isFilter) {
        return [searchArray count];
    } else {
        return [self.EtNotifRepTableDataArray count];
    }
}

Try and share your results.