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

swac

macrumors newbie
Original poster
Oct 25, 2007
21
0
I'm learning how to make applications Cocoa, and the first app I'm trying to make is a basic four-function calculator. I'm having trouble getting my initialization method to run so that I can initialize a few objects and other variables. I tried looking up an initialization method, and the only one I found was

+ (void)initialize

The description of this method seems to be what I'm looking for, so I added in what I wanted to add, yet it doesn't seem to be executing that method when I launch the program. Am I using the method incorrectly/is there some other method that I should be using?

Thanks!
 
Generally you'd run most of your app's initialization either from -awakeFromNib (for objects in nib files) or NSApp's delegate in -applicationWillFinishLaunching:
 
+initialize is called before the class or object receives its first message. This likely isn't the place you want to initialize variables for your objects, since you can't get access to ivars from a class method. -init, -awakeFromNib, or -applicationDidFinishLaunching: are typical places for doing this.
 
I usually call -init to do all my initializations. I used to put everything in -awakeFromNib, but I feel like a noob putting everything in there. Use something like this to initialize your variables.
Code:
- (id)init {
    if(self = [super init]) {
        //your initialization code goes here
    }

    return self;
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.