mutating方法发送到不可变对象

时间:2011-04-26 12:58:59

标签: ios objective-c cocoa cocoa-touch

当我第一次使用这个方法时它工作正常,但是当我第二次调用它时,我得到了错误“mutating method sent to immutable object”。问题与“addObject”命令一致。

-(IBAction) save: (id) sender{

NSMutableArray *placesT= [[NSUserDefaults standardUserDefaults] objectForKey:@"placesT"];

if (!placesT) {
    placesT=[[[NSMutableArray alloc] init] autorelease];
}

[placesT addObject: [NSString stringWithFormat:@"%@", tagF.text] ];

NSUserDefaults *tUD=[NSUserDefaults standardUserDefaults];
[tUD setObject:placesT forKey:@"placesT"];
[tUD synchronize];

[self dismissModalViewControllerAnimated:YES];

}

4 个答案:

答案 0 :(得分:24)

正如NSUserDefaults的文档所说:“即使您将可变对象设置为值,NSUserDefaults返回的值也是不可变的。”每当您想要更改从NSUserDefaults获得的集合时,您必须获取不可变版本,创建mutableCopy,修改它,然后重新设置它。

答案 1 :(得分:10)

这是因为存储在NSUserDefaults中的对象不是mutableArray而是普通数组。

- (IBAction)save:(id)sender {

   NSMutableArray *placesT = nil;
   NSArray *tempArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"placesT"];

   if (tempArray) {
      placesT = [tempArray mutableCopy];
   } else {
      placesT = [[NSMutableArray alloc] init];
   }

   [placesT addObject:[NSString stringWithFormat:@"%@", tagF.text]];

   NSUserDefaults *tUD = [NSUserDefaults standardUserDefaults];
   [tUD setObject:placesT forKey:@"placesT"];
   [tUD synchronize];

   [self dismissModalViewControllerAnimated:YES];
   [placesT release];
}

答案 2 :(得分:3)

placesT是一个非可变数组,要么始终将placesT设置为可变对象,要么使用以下代码。

NSMutableArray *placesT= [[[NSUserDefaults standardUserDefaults] objectForKey:@"placesT"] mutableCopy];

答案 3 :(得分:0)

这应该有效:

  

- (IBAction)save:(id)sender {

NSMutableArray *placesT= [[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults]
     

objectForKey:@ “placesT”]];

if (!placesT) {
    placesT=[[[NSMutableArray alloc] init] autorelease];
}

[placesT addObject: [NSString stringWithFormat:@"%@", tagF.text] ];

NSUserDefaults *tUD=[NSUserDefaults standardUserDefaults];
[tUD setObject:placesT forKey:@"placesT"];
[tUD synchronize];

[self dismissModalViewControllerAnimated:YES]; }