views:

141

answers:

2

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]?

+3  A: 

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.)

KennyTM
I'm getting the same error with this method.
Peter George
@Pater: see update.
KennyTM
Thanks, but I still have the same issue. I imported BaseViewController.h and inherited FirstViewController: BaseViewController in the FirstViewController.h, and the same for the rest of the controllers. Also I created the BaseViewController files which inherits from a normal UIViewController
Peter George
@Peter: What does your code looks like now?
KennyTM
I forgot to add the property to the baseViewController, it works now thanks a lot for your patience ;)
Peter George
A: 

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;

texmex5
This kinda works, but eleminates the use of my array, in which I wanted to set all viewcontroller inside the array with properties.
Peter George
You can still do this within the for. With best scenario all your viewcontrollers would extend a "ZeroViewController" with managedObjectContext property and you could then cast as that in the for. But I think that if all your viewcontrollers (no matter what the class name) have that property then it wouldn't matter how you cast it in the for, as long as the class has the property.
texmex5