您好我正在尝试为嵌套在NSMutableArray中的viewcontroller设置属性, 例如,我在这个数组中有3个ViewController:
FirstViewController *firstViewController = [FirstViewController alloc];
SecondViewController *secondViewController = [SecondViewController alloc];
ThirdViewController *thirdViewController = [ThirdViewController alloc];
NSMutableArray *viewControllerClasses = [[NSMutableArray alloc] initWithObjects:
firstViewController,
secondViewController,
thirdViewController,
nil];
for (int x=0; x<[viewControllerClasses count]; x++) {
// as an example to set managedObjectContext I otherwise would set firstViewController.managedObjectContext = context;
[viewControllerClasses objectAtIndex:x].managedObjectContext = context;
}
但是这会导致错误:请求成员“managedObjectContext”,而不是结构或联合。 不应该是“firstViewController”和[viewControllerClasses objectAtIndex:0]相同吗?
答案 0 :(得分:3)
-objectAtIndex:
方法返回id
,由于编译器无法确定getter,因此无法应用点语法。
无论如何,你应该只使用快速枚举(for
/ in
循环),你可以给出一个静态类型,这样可以使用点语法(假设FirstViewController和其余的继承自BaseViewController) ):
for (BaseViewController* ctrler in viewControllerClasses)
ctrler.managedObjectContext = context;
此外,您可以恢复使用括号:
for (BaseViewController* ctrler in viewControllerClasses)
[ctrler setManagedObjectContext:context];
(并在-init…
之后立即+alloc
。)
答案 1 :(得分:0)
您可以尝试投射[viewControllerClasses objectAtIndex:x]
返回没有NSObject
属性的任意managedObjectContext
,因此请尝试使用((FirstViewController *)[viewControllerClasses objectAtIndex:x]).managedObjectContext = context;