Ryan Minnick Ryan Minnick - 1 year ago 75
iOS Question

do I need a main method with Objective-C

I am learning Objective-C currently. I am already fluent in Swift syntax (Not to be confused with Actual app creation, that I'm pretty new with) barely getting started, I've noticed something quite frustrating and confusing happen... I am barely learning the basics right now so bare with me:

When I create a string variable in the Main class like so:

NSString *str;

it works. But when I attempt to do it outside of the main class, it returns an error about returning a default integer value or something ridiculous. Do I always need a main method and need to put all code inside of the main method? What about for other classes? Do I just copy and paste the main method to the new class? Just to be clear I am referring to this:

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
return 0;

Answer Source

All Objective C (and C) based programs start from the main method. However, you just need one main method. You can call objects, functions, etc from the main method, but it always starts at main because the program needs to know where to begin.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download