talking methods in same class

Discussion in 'Mac Programming' started by Darkroom, Nov 26, 2008.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    BLURG!!!

    how do i tell one method to goto another method in the same class? i know it's so simple it's dumb, but i can never get it right...

    Code:
    - (IBAction)writeSomething:(id)sender
    	{
    	NSLog( @"Apples ");
    	[self secondPart:nil];
    	}
    	
    - (void)secondPart
    	{
    	NSLog (@"and ");
    	[self thirdPart:nil];
    	}
    	
    - (void)thirdPart
    	{
    	NSLog (@"Oranges");
    	}
    
     
  2. Nishad macrumors newbie

    Joined:
    Aug 2, 2007
    #2
    [self secondPart:nil];

    here you are calling the method 'secondPart' with a parameter. but there is no such method.

    correct way to call the method is

    [self secondPart];

    remaining also same.
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    Same idea, but thirdPart also has no parameters.

    -Lee
     
  4. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #4
    Also, you use the word "go to"; be warned that you're only "visiting" that other method. Once it finishes, it will return to the line after the function call.

    You probably know this, but just in case. "go to" is an existing programming concept with different semantics - might as well get your terminology right.
     
  5. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #5
    ok... so if the second method was called "-(IBAction)secondPart:(id)sender", would the correct way to call it be this: [self secondPart:sender]?

    -or-

    if the second method was called "-(void)secondPart:(NSTimer *)thatTimer", would the correct way to call it be this: [self secondPart:thatTimer]?
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    Sure. In either of those your invocation with nil would work, too, as long as they could deal with this. It may be slightly strange to call another IBAction that way, but it's not "wrong".

    -Lee
     
  7. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #7
    humm... ok, so if i'm trying to access another method in the same class i just have to invoke ":nil" if the method i'm trying to access has... um... arguments?

    so if i have some crazy method with several arguments like this:

    - (void)imageBrowser:(IKImageBrowserView*)view removeItemsAtIndexes:(NSIndexSet*)indexes

    i could call it by writing [self imageBrowser:nil]? or would it be [self imageBrowser:nil removeItemsAtIndexes:nil] since the method i'm calling has 2 names/arguments?

    i should state that i'm not really trying to call this image browser method. i'm just using this method name as an example.
     
  8. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    The short answer is 'yes'.
    Nil is essentially a null pointer. It can be cast to any pointer type. It would be better not to use nil for types that are not pointers. It may cast the 0 to the type you want, but it will be confusing.

    -Lee
     
  9. mkelly11 macrumors newbie

    Joined:
    Dec 1, 2008
    #9
    Actually, goto can only be used to jump out of a scope in the current function. You can't go jumping around from function to function using goto... I found that out the long way =(
     
  10. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #10
    Interestingly, both Visual Studio 2005 and gcc 3.4.3 simply error out when you try to jump to a label in a different function (I'm too lazy to try other compilers right now).

    Also interestingly, you can jump into "the middle of" a scope:

    Code:
    int main(void)
    {
        goto label1;
        {
            int a = 42;
    label1:
            return a;
        }
    }
    
    Visual Studio 2005 gives a warning here that "a is used uninitialized", but gcc 3.4.3 with -Wall doesn't see this as something noteworthy. Strange.

    Anyway, this is all academic. Please don't do this stuff in real code.
     
  11. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #11

Share This Page