iPhone 设置 ViewController 嵌套在 NSMutableArray 中

发布于 08-31 23:49 字数 1064 浏览 7 评论 0原文

您好,我正在尝试为嵌套在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冧九2024-09-07 23:49:46

-objectAtIndex: 方法返回一个 id,但不能应用点语法,因为编译器无法确定 getter。

无论如何,您应该只使用快速枚举(for/in 循环),并且您可以给出静态类型,以便可以使用点语法(假设 FirstViewController 和其余部分)继承自 BaseViewController):

for (BaseViewController* ctrler in viewControllerClasses)
   ctrler.managedObjectContext = context;

此外,您可以恢复使用括号:(

for (BaseViewController* ctrler in viewControllerClasses)
   [ctrler setManagedObjectContext:context];

请在 +alloc 之后立即使用 -init...。)

The -objectAtIndex: method returns an id, 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):

for (BaseViewController* ctrler in viewControllerClasses)
   ctrler.managedObjectContext = context;

Also, you could revert to use brackets:

for (BaseViewController* ctrler in viewControllerClasses)
   [ctrler setManagedObjectContext:context];

(and please -init… immediately after +alloc.)

丢了幸福的猪2024-09-07 23:49:46

您可以尝试转换 [viewControllerClasses objectAtIndex:x] 返回一个没有 managedObjectContext 属性的任意 NSObject ,因此请尝试使用 ( (FirstViewController *)[viewControllerClasses objectAtIndex:x]).managementObjectContext = context;

You may try casting [viewControllerClasses objectAtIndex:x] returns an arbitary NSObject that doesn't have managedObjectContext property, so try using ((FirstViewController *)[viewControllerClasses objectAtIndex:x]).managedObjectContext = context;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文