Reputation: 12184
I have seen the Apple's example of Singleton and couple of other examples. People say that it is too strict!
But the point is ..even if it is too strict, I want to understand it. I dont understand that when we call allocWithZone on super, What happens ?
Memory will be created according to super's instance size.
What if our Singleton has ivars ?
I also dont understand that, Why does allocWithZone return the object with a retain call when retain itself is returning the object as it is.
Upvotes: 0
Views: 113
Reputation: 53000
Whenever a method is called in Obj-C it is passed an object reference via the hidden parameter self
. For an instance method self
refers to the object the method was invoked on, for a class method self
refers to the the class object (of type Class
) the method was invoked on. Calls to super
implicitly pass on self
.
Therefore in Apple's example code the call [super allocWithZone:NULL]
calls the super implementation of allocWithZone
passing the current value of self
, which is MyGizmoClass
's class object as it is a static method. The implementation of allocWithZone
can determine the required memory size from the passed Class
object – the details of how are private.
As you've correctly spotted, the call to retain
in allocWithZone
is pointless but harmless.
Upvotes: 1