i was wondering when we use the “init” method in a project? i have seen many projects without it, they used “applicationDidFinishLaunching” to start the application, but not the init method, do you have any idea about that?
Thanks a lot
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
initmethod is used to initialize an object after allocating it.In case it is not defined, then the base class implementation is called, as with any other method in Objective-C, and if the base class has got no
initmethod, then the base class’ base class’initwill be called, and so on, up to theNSObject‘sinitwhich is for sure provided.If you don’t declare an
initmethod, then you will not be able to properly initialize the ivars in your class, if any.About
applicationDidFinishLaunching, this is a method for a very special class that any Cocoa/iOS application has got, its application delegate. For example:(N.B.:
UIApplicationDelegatecould be as wellNSApplicationDelegate)As you see, this class derives from NSObject, so, it will be inited by the framework by calling an
initmethod defined in that class (which one specifically depends on the framework). Then, at some point, the framework will call theapplicationDidFinishLaunchingmethod and you have a change to initialize all your delegates’ members and do whatever you need to start your app up.In this case, the fact that you are not required to do initialization in an
initmethod but inapplicationDidFinishLaunchingis related to a specific pattern of interaction between your UIApplication (or NSApplication) object and its delegate. This pattern was defined by Apple in this way;you specify your application delegate class either in your MainWindow.xib or when calling
UIApplicationMaininmain.c;the framework is responsible to instantiate those class (in a custom way that programmers do not need to deal with) and call
applicationDidFinishLaunchingso that you can do whatever you need with it.I hope this explanation helps you understanding what is going on with the app delegate and why you do not declare an
initmethod.