tags:

views:

41

answers:

1
- (void) swapController:(MyViewController*)controller1 
                         with:(MyViewController*)controller2
{
    MyViewController *swap = controller2;
    controller2 = controller1;
    controller1 = swap;
}

Looks like this doesn't work because I'm not passing references. How to do it anyway?

+3  A: 

You can do this by passing in pointers to the pointers you want to change:

- (void)swapController:(MyViewController**)controller1 with:(MyViewController**)controller2
{
    MyViewController* swap = *controller2;
    *controller2 = *controller1;
    *controller1 = swap;
}
Huw Giddens
Michael
@Michael: That is right, though I cannot imagine why you would want a generic instance method to swap two variables.
Chuck
@Chuck: it's all about var names.
Michael