77

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?

jrdioko
  • 32,230
  • 28
  • 81
  • 120

2 Answers2

126

-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.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • 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)). –  May 31 '11 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 '11 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 '14 at 05:14
  • 3
    @makaed Your question is answered nicely in the [docs](http://bit.ly/1eSNpGI) -- 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 '14 at 06:38
  • 1
    Ah, thanks! Now that makes sense. I also created a [question](http://stackoverflow.com/questions/21200377/implementation-of-init-vs-initialize) and had some good answers. – makaed Jan 18 '14 at 06:41
19

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

Eki
  • 316
  • 3
  • 14