- [__ NSArrayI length]:尝试使用searchController进行过滤时崩溃

时间:2015-01-21 03:54:38

标签: ios nsarray uisearchdisplaycontroller searchbar

我尝试使用searchController进行过滤。我在以下代码中的-[__NSArrayI length]: unrecognized selector sent to instance行遇到了崩溃(NSRange productNameRange = NSMakeRange(0, [[_list valueForKey:@"name"] length]);):

-(void)updateFilteredContent:(NSString*)searchText {
    // Update the filtered array based on the search text and scope.
    [self.searchResults removeAllObjects];
    // Filter the array using NSPredicate
    NSLog(@"_categories are %@",_categories);
    for (NSDictionary *object in _categories) {
        NSUInteger searchOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch;
        NSRange productNameRange = NSMakeRange(0, [[_list valueForKey:@"name"] length]);
        NSRange foundRange = [[_list valueForKey:@"name"] rangeOfString:searchText options:searchOptions range:productNameRange];
        if (foundRange.length > 0) {
            [self.searchResults addObject:object];
        }
    }
}

我尝试在if ([[object valueForKey:@"name"] length] >0){之类的长度上添加if语句,但它仍然崩溃。

_list的日志示例是:

_list names are (
    "Honda",
    "Ford",
    Toyota,

2 个答案:

答案 0 :(得分:0)

你的[_list valueForKey:@" name"]返回一个NSarray,它应该是一个NSString来计算它的长度..

答案 1 :(得分:0)

请检查此情况,并告诉我它是否仍然崩溃!

if([_list allkeys] contains @"name"])
{
  NSString *tempStr = [_list valueForKey:@"name"];
}
if(tempStr)

NSRange productNameRange = NSMakeRange(0, [tempStr length]);