0

我在下面方法的行中有一个异常,[array removeObjectsInArray:toRemove];无法理解它有什么问题..

- (void) handleDearchForTerm:(NSString *)searchTerm
{
    NSMutableArray *sectionsToRemove = [[NSMutableArray alloc] init];
    [self resetSearch];
    for (NSString *key in _keys)
    {
        NSMutableArray *array = [_names valueForKey:key];
        NSMutableArray *toRemove = [[NSMutableArray alloc] init];
        for (NSString *name in array)
        {
            if ([name rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location == NSNotFound)
                [toRemove addObject:name];
        }
        if ([array count] == [toRemove count])
            [sectionsToRemove addObject:key];
        [array removeObjectsInArray:toRemove];
    }
    [_keys removeObjectsInArray:sectionsToRemove];
    [_table reloadData];
}
4

2 回答 2

3

可能数组只是一个实例NSArray,但不是NSMutableArray,你应该检查_names setObject:forKeys:

于 2013-09-17T11:25:04.633 回答
1

改变:

 NSMutableArray *array = [_names valueForKey:key];

至:

 NSMutableArray *array = [[NSMutableArray alloc] initWithArray:[_names valueForKey:key]];

或者:

 NSMutableArray *array = [[_names valueForKey:key] mutableCopy];
于 2013-09-17T11:57:52.487 回答