Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 为什么我会得到这样的回答;“发送到不可变对象的变异方法”;尝试从MutableArray中删除时?_Objective C_Cocoa_Cocoa Touch - Fatal编程技术网

Objective c 为什么我会得到这样的回答;“发送到不可变对象的变异方法”;尝试从MutableArray中删除时?

Objective c 为什么我会得到这样的回答;“发送到不可变对象的变异方法”;尝试从MutableArray中删除时?,objective-c,cocoa,cocoa-touch,Objective C,Cocoa,Cocoa Touch,我不明白为什么我在这段代码中得到了“发送到不可变对象的变异方法”。数组一定是不可变的,但我不知道为什么 接口: @interface SectionsViewController : UIViewController<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate> { UITableView *table; UISearchBar *search; NSMutableD

我不明白为什么我在这段代码中得到了“发送到不可变对象的变异方法”。数组一定是不可变的,但我不知道为什么

接口:

    @interface SectionsViewController : UIViewController<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate> {

    UITableView *table;
    UISearchBar *search;
    NSMutableDictionary *names;
    NSMutableArray *keys;

}
@property (nonatomic, retain) IBOutlet UITableView *table;
@property (nonatomic, retain) IBOutlet UISearchBar *search;
@property (nonatomic, retain) NSDictionary *allNames;
@property (nonatomic, retain) NSMutableDictionary *names;
@property (nonatomic, retain) NSMutableArray *keys;

-(void) resetSearch;
-(void) handleSearchForTerm:(NSString *)searchTerm;

@end
以下是完整上下文中的方法:

-(void)handleSearchForTerm:(NSString *)searchTerm
{
    NSMutableArray *sectionsToRemove = [[NSMutableArray alloc] init];
    for(NSString *key in self.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];
        [toRemove release];
    }
    [self.keys removeObjectsInArray:sectionsToRemove];
    [sectionsToRemove release];
    [table reloadData];
}
我根据[names valueForKey:key]的结果分配数组; 数组的类型为“MutableArray”,我缺少什么


谢谢

变量静态类型为
NSMutableArray
,但分配给变量的对象似乎不是
NSMutableArray
。变量的类型只是编译器在进行类型检查和选择方法签名时要使用的一个提示-您仍然需要注意确保您实际分配了变量所表示的应包含的类型。

valueForKey:
返回一个
NSArray
。将其发送到
NSMutableArray
并不重要

您可以将结果强制转换为
(NSMutableArray*)
,但我个人的偏好是获得一份副本:

NSMutableArray *array = [[[names valueForKey:key] mutableCopy] autorelease];

请记住,您需要
autorelease
release
发布
mutableCopy
的结果;为了方便起见,我编辑了
autorelease
模式。如果该键的值设置为可变数组,它将返回可变数组。您所说的仅适用于通过KVC访问的“有序到多”关系(在这种情况下,mutableArrayValueForKey:将是一种方式)。无论如何,强制转换返回的指针(即使有必要避免编译器警告,它不是valueForKey返回id)不会影响它引用的对象的运行时行为。
valueForKey:
在任何情况下都不一定返回不可变的对象;当发送到字典(例如
名称
)时,它将返回您最初放入字典中的对象,而不管字典的易变性如何。将不可变数组放入可变字典意味着您将从中获得相同的不可变数组。
NSMutableArray *array = [[[names valueForKey:key] mutableCopy] autorelease];