[__NSCFString objectForKey:]:文本字段搜索时崩溃

[__NSCFString objectForKey:]: crash on textfield search

我在 table 视图中搜索数据时遇到此崩溃。我正在使用文本字段作为搜索栏。这是我的代码:

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;
}

我需要搜索数字和字符串。但它因以下错误而崩溃:

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

在我的 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];
        }
    }

你用这个替换你的 cellForRowAtIndexPath 方法。

- (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;
}

问题是您直接在 cellForRowAtIndexPath 方法的过滤器部分中获取对象,该方法返回一个 NSString 值,然后您再次尝试从此 NSString 获取 objectForKey 这会导致崩溃,因为 NSString 没有已知方法 objectForKey

编辑:

这与崩溃无关,但您还应该根据过滤器更新此方法。

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

尝试并分享您的结果。