views:

260

answers:

2

This is Objective-C, in Xcode for the iPhone.

I have a method in main.m:

int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

//I want to call the method here//

int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}

static BOOL do_it_all () {
//code here//
}

How do I call the do_it_all method from main.m?

+2  A: 

Like this:

do_it_all();

It's just an ordinary C function call. But you will either need to move the declaration of do_it_all before main, or forward declare it; otherwise, main won't know what you're talking about.

Jon Reid
How do I forward declare it. I thought you could only forward declare a class.
AaronG
Forward declaration of an Objective-C class (with `@class`) is different. In this case we're talking about a function prototype.
Quinn Taylor
+3  A: 

You can call it normally, so long as you've already declared the function before you call it. Either move the function definition above main() or add the following line above:

static BOOL do_it_all ();

Personally, I think the former is easier, but if you have circular dependencies between functions, it can be impossible to resolve without function prototypes.

When you do add function prototypes in C/Objective-C/etc. they are frequently in a header (.h) file, but if everything is in main.m this is probably overkill.

Quinn Taylor
I did have circular dependencies. This information has been very helpful. Thank you.Working in main.m seems to be jus a little bit different than standard .h/.m files.
AaronG