blockaj
blockaj

Reputation: 329

Where do I put code in an Objective-C class if I want it to run as soon as the class is called?

Sorry. This is a really basic question but I can't find the answer anywhere. I just want to cast variables as soon as an iOS app starts up so I don't have too much in the button action.

Upvotes: 1

Views: 96

Answers (2)

Lukman
Lukman

Reputation: 19164

If you want to perform some operations on class level rather than instantiation level, you can either overload + (void)load or + (void)initialize:

+ (void)load {
    // codes will run as soon as the application is loaded into the runtime
}

+ (void)initialize {
    // codes will run the first time this class is being referred to in the application execution
}

Upvotes: 6

Skyler
Skyler

Reputation: 2864

If you mean "instantiated" instead of "called", override - (id)init in the .m implementation file of the class, like so:

- (id)init {

     if (self = [super init]) {
          // your custom code here
     }
     return self;
}

Upvotes: 0

Related Questions