开发者

Only Calling Alloc on the Object

开发者 https://www.devze.com 2023-02-22 11:21 出处:网络
I am using the following code to initialize my 开发者_运维技巧object: Spaceship *spaceship = [Spaceship alloc];

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号