【问题标题】:[__NSCFString objectForKey:]: crash on textfield search[__NSCFString objectForKey:]:文本字段搜索崩溃
【发布时间】:2019-02-11 07:07:38
【问题描述】:

当我在表格视图中搜索数据时,我遇到了这个崩溃。我使用文本字段作为搜索栏。这是我的代码:

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

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

*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFString objectForKey:]: 无法识别的选择器发送到实例 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];
        }
    }

【问题讨论】:

  • 请说明_EtNotifRepTableDataArray是如何定义和填充的
  • 我正在使用 sqlite 来保存数据。请检查我的帖子。我已经更新了
  • 似乎arr 这里for (NSDictionary *dict in arr) 包含NSString 对象而不是NSDictionary。你能在你的问题中添加arr的日志吗?
  • [__NSCFString objectForKey:] 表示您正在为 NSString 对象调用 objectForKey。尽管您声明了 NSDictionary “dict”,但不知何故“dict”在您的一个代码中保存了一个 nsstring,因此它会导致此错误。你需要一步一步的 NSLog 来调试它,看看你在哪里没有得到字典。

标签: ios arrays objective-c nsdictionary uisearchbar


【解决方案1】:

你用这个替换你的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];
    }
}

尝试并分享您的结果。

【讨论】:

  • 这样我只能搜索我的名字,而不是我的号码,对吗?我如何搜索我的@"Qmnum@"Eqktx" ..字符串和数字
  • 用我上面发布的那个替换你现有的 cellForRowAtIndexPath 之后。崩溃修复了吗?检查我的编辑。
猜你喜欢
  • 1970-01-01
  • 2015-12-20
  • 2012-04-02
  • 1970-01-01
  • 2023-01-25
  • 1970-01-01
  • 2015-01-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多