iPhone 设置 ViewController 嵌套在 NSMutableArray 中
您好,我正在尝试为嵌套在 NSMutableArray 内的视图控制器设置属性, 例如,我在这个数组中有 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] 相同吗?
Hello I'm trying to set attributes for a viewcontroller nested inside a NSMutableArray,
for example I have 3 ViewController inside this array:
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;
}
But this results in an error: Request for member "managedObjectContext" in something not a structure or union.
Shouldn't be "firstViewController" be the same as [viewControllerClasses objectAtIndex:0]?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
-objectAtIndex:
方法返回一个id
,但不能应用点语法,因为编译器无法确定 getter。无论如何,您应该只使用快速枚举(
for
/in
循环),并且您可以给出静态类型,以便可以使用点语法(假设 FirstViewController 和其余部分)继承自 BaseViewController):此外,您可以恢复使用括号:(
请在
+alloc
之后立即使用-init...
。)The
-objectAtIndex:
method returns anid
, which the dot-syntax cannot be applied on because the compiler can't determine the getter.Anyway, you should just use fast enumeration (
for
/in
loop), and you could give a static type so the dot-syntax can be used (assuming FirstViewController and the rest inherits from BaseViewController):Also, you could revert to use brackets:
(and please
-init…
immediately after+alloc
.)您可以尝试转换
[viewControllerClasses objectAtIndex:x]
返回一个没有managedObjectContext
属性的任意NSObject
,因此请尝试使用( (FirstViewController *)[viewControllerClasses objectAtIndex:x]).managementObjectContext = context;
You may try casting
[viewControllerClasses objectAtIndex:x]
returns an arbitaryNSObject
that doesn't havemanagedObjectContext
property, so try using((FirstViewController *)[viewControllerClasses objectAtIndex:x]).managedObjectContext = context;