Only Calling Alloc on the Object
I am using the following code to initialize my 开发者_运维技巧object:
Spaceship *spaceship = [Spaceship alloc];
[spaceship fire];
The fire method is getting invoked and it seems to be working fine. My question is that how is it working when I have not used the init method to initialize the object.
What does your -init
method actually do? If you don't have one, I'll let you into a secret: in the current implementation of the run time, NSObject
's -init
is just about a no-op, so forgetting to send -init
to an object with no -init
method of its ownis not a huge disaster. You shouldn't rely on that though and you should be sending -init
to your objects.
The fire method will work because objective-c just pass the message to the receiver. But any initialization that you do in your init is not executed.
精彩评论