2012-11-01 29 views
0

檢索對象出於某種原因,我不能指定我從可變數組檢索無法分配從NSMutableArray的

更新對象:請注意,我使用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

[[FormRenderManager sharedInstance] .formControls'總是爲每個調用返回相同的數組還是一個新的自動發佈的實例? – 2012-11-01 09:11:56

+0

我已更新我的問題,指出我正在使用ARC。 [FormRenderManager sharedInstance] .formControls將始終返回相同的數組。 – RynoB

+0

謝謝!在這種情況下非常奇怪......我會更多地考慮它。 – 2012-11-01 09:19:36

回答

1

filteredArrayUsingPredicate返回一個不可變數組。你將不得不這樣做:

[NSMutableArray arrayWithArray:[array filtered...]]; 
+0

謝謝,這個伎倆! – RynoB