Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
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");
	}
 

Nishad

macrumors newbie
Aug 2, 2007
11
0
[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.
 

Sander

macrumors 6502a
Apr 24, 2008
521
67
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.
 

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
[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.

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

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
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

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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

mkelly11

macrumors newbie
Dec 1, 2008
7
0
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.

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 =(
 

Sander

macrumors 6502a
Apr 24, 2008
521
67
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 =(

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.