azamsharp
azamsharp

Reputation: 20094

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.

Upvotes: 0

Views: 75

Answers (2)

JeremyP
JeremyP

Reputation: 86691

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.

Upvotes: 2

Black Frog
Black Frog

Reputation: 11725

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.

Upvotes: 2

Related Questions