PDA

View Full Version : In Init, does self refer to the class or the instance?




teguh123
Apr 7, 2011, 09:13 PM
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?



Catfish_Man
Apr 8, 2011, 01:09 PM
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.

chown33
Apr 8, 2011, 01:28 PM
Cocoa Fundamentals Guide (http://developer.apple.com/library/mac/#documentation/cocoa/Conceptual/CocoaFundamentals/Introduction/Introduction.html%23//apple_ref/doc/uid/TP40002974)

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