A note about init methods


/ Published in: iPhone
Save to your folder(s)

Here is a breakdown of creating a custom initializer and or general rules of init


Copy this code and paste it in your HTML
  1. In summary, this is what an init method needs to do:
  2.  
  3. - Call [super init] and assign the result to self.
  4. - Check whether self is nil. If so, then exit this method right away and return nil to the caller.
  5.  
  6. - If self was not nil, do additional initialization if necessary. Usually this means you give properties and instance variables their initial values. By default, objects are nil, ints are 0 and BOOLs are NO. If you want to give these variables different initial values, then this is the place to do so.
  7. - Return self to the caller.
  8.  
  9. You don’t always need to provide an init method. If your init method doesn’t need to do anything — if there are no properties or instance variables to fill in — then you can leave it out completely and the compiler will provide one for you.

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.