I’m doing some programming and I’m also reading some code at the same time and I come across this code
-(id)init
{
if ((self = [super init]))
{
some code....
[self initEnemy];
}
return self;
}
and below that it has
-(void)initEnemy
{
more code....
[self resetEnemy];
}
and then..
-(void)resetEnemy
{
more code.. etc..
}
The way I see it is the first method called init calls on the method initEnemy and that in turn calls on resetEnemy. Basically one method brings on the other and so on.
Successfully forming an algorithm (you can’t really tell because i’ve shown little code). Am I looking at it the right way?
Also, could I have an explanation on what happens inside the -(id)init method when return self; is performed.
Yup, you’re reading that sequence of execution correctly.
When “return” happens in a method, control returns to the “caller” method (whoever originally called it in the first place). The “self” is there to indicate that the value of “self” should be handed back to the caller. (In this case, “self” refers to the instance of the object being initialized in the
-init. If you want to know more about initializers, you can break it down into more specific questions.)