78

In Objective-C, what is the difference between the init method (i.e. the designated initializer for a class) and the initialize method? What initialization code should be put in each?

2 Answers 2

125

-init is an instance method, used to initialize a particular object. +initialize is a class method, run before any instances of the class are created and before other class methods are run. +initialize isn't something you use most of the time, but it's handy for setting up any static variables that the class as a whole might use, or for ensuring that certain conditions are met before any instances are created.

The code that belongs in an -init method is described thoroughly in the Implementing an Initializer section of The Objective-C Programming Language. There's also some discussion of initializing classes (i.e. +initialize) and why you might need to do that in the same document, in the Class Objects section. The code that goes into +initialize will generally be strongly tied to the special functionality of the class that requires you to initialize it in the first place. One important thing to keep in mind in +initialize (and in any class method) is that self in a class method refers to the class itself, not an instance of the class.

5
  • 12
    "run before any instances of the class are created" not really: sent before that class receives it's first message (excluding the +initialize method). You can, in fact create an instance of a class without sending that class a message (with the IMP of it's create-instance method (usually +allocWithZone: but this can vary)).
    – user142019
    May 31, 2011 at 18:08
  • 11
    @WTP, fair point, but if you're operating in that space you're probably pretty clear on the distinction between +initialize and -init already.
    – Caleb
    May 31, 2011 at 18:10
  • 4
    I usually see if (self == [NameOfClass class]) inside +initialize method, why do we need to check that? I know it's something to do with +initialize method being called on parent class twice, but it's not clear for me. Any help appreciated!
    – makaed
    Jan 18, 2014 at 5:14
  • 3
    @makaed Your question is answered nicely in the docs -- here's the relevant line: The superclass implementation may be called multiple times if subclasses do not implement initialize—the runtime will call the inherited implementation... So, even though +initialize is called only once per class, a single implementation might be called several times -- once for the class in which it's and once for each subclass that doesn't provide it's own +initialize method.
    – Caleb
    Jan 18, 2014 at 6:38
  • 1
    Ah, thanks! Now that makes sense. I also created a question and had some good answers.
    – makaed
    Jan 18, 2014 at 6:41
19

To draw a parallel for Java developers, init is like a constructor, while initialize is like a static block on a class.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.