In Init, does self refer to the class or the instance?

Discussion in 'Mac Programming' started by teguh123, Apr 7, 2011.

  1. macrumors member

    Mar 22, 2011
    Also is there any case where we call alloc and not init after that?

    What confuses me is things like

    self= [super init]

    Basically, if self is a pointer to the class, we're tell self to point to the address described by [super init]

    The problem with that is self should already point to whatever address returned by alloc right?

    So why change it?
  2. macrumors 68030


    Sep 13, 2001
    Portland, OR
    In any instance method (-), self refers to the instance. In any class method (+), self refers to the class. Always call an init method after calling +alloc. The reason to set it is that some classes play tricks in -init and don't return the same object that +alloc did.
  3. macrumors 604

    Aug 9, 2009
    Cocoa Fundamentals Guide

    In particular, read the sub-section on "Basic Subclass Design". Ideally, you should read the entire guide.

Share This Page