由于某种原因,我无法分配从可变数组中检索的对象
更新:请注意,我正在使用ARC
以下是代码:
id<Control> control = [formControlUtils getControlWithId:@"123"];
// Here control.controlValue is "Old value"
control.controlValue = @"New value";
// Even after assigning a new value to the property the value is still "Old value"
- (id<Control>)getControlWithId:(NSString *)controlId {
id<Control> control = nil;
for (NSArray *array in [FormRenderManager sharedInstance].formControls)
{
//[FormRenderManager sharedInstance].formControls is a mutable array, so is the nested arrays
control = [[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"controlId = %@", controlId]] lastObject];
if (control)
break;
}
return control;
}
正如您在上面的代码注释中所看到的,每当为control.controlValue分配新值时,旧值仍然存在。
为什么?我是否可能会错过一些基本的东西,或者是因为我违反了协议<id>Control control
?
答案 0 :(得分:1)
filteredArrayUsingPredicate
返回一个不可变数组。你必须这样做:
[NSMutableArray arrayWithArray:[array filtered...]];