I think I almost have an understanding of the 'Self'. From Jim's explanations, Lunda.com video and other resources. There are a few questions I still have.
1) From Main.m, I create an object from a class and alloc and init that object. In this example 'myClass* myObject = [[myClass alloc]init]'
2) When I alloc/init the 'myObject', instance variables are created (if defined) but nothing happens with the methods until a Method is sent a message to do something from Main.m, in this example.
3) Here is what i'm not sure of yet. If myClass has a method definition named -(id)init. Inside of this method I have a line of code, lets say 'score = 10'.
So in Main.m, when I say [myClass* myObject] it will then create myObject and while doing so if it sees a method, in the myClass called '-(id)init' the compiler says "ahhh, this means he wants the object, myObject, to be initialized with with the score equal to 10."
Correct me if I am wrong. I send messages to methods to do something. But I don't send a message to a method called-(id)init. Like my new car I got. Ford created that Object and there was a method that was
}
Now my fordObject already has instance variables initialized when it is created. This then saves me the step of writing code in Main.m like [fordObject gasGallons: 18] and so on. Is this understanding right?
So in the end, I am getting 1 object back to use 'fordObject', with the instance variables already initialized to a value, right? Or, I read, and you guys have mentioned that id is a generic object. when -(id)init method is executed, a second object is created too, fordObject and idObject? In this case idObject would contain the initialized values and not fordObject?
Is id going to be it's own object or is it apart of fordObject?
Thanks again. I think I almost got this.
O' PS. I tried to dumb down my terminology to make it simple. I think it should have been [fordClass *exploreObject] and [fordClass* focusObject] and so on.
1) From Main.m, I create an object from a class and alloc and init that object. In this example 'myClass* myObject = [[myClass alloc]init]'
2) When I alloc/init the 'myObject', instance variables are created (if defined) but nothing happens with the methods until a Method is sent a message to do something from Main.m, in this example.
3) Here is what i'm not sure of yet. If myClass has a method definition named -(id)init. Inside of this method I have a line of code, lets say 'score = 10'.
Code:
-(id)init{
self = [super init];
score = 10;
return self;
}
So in Main.m, when I say [myClass* myObject] it will then create myObject and while doing so if it sees a method, in the myClass called '-(id)init' the compiler says "ahhh, this means he wants the object, myObject, to be initialized with with the score equal to 10."
Correct me if I am wrong. I send messages to methods to do something. But I don't send a message to a method called-(id)init. Like my new car I got. Ford created that Object and there was a method that was
Code:
-(id)init{
self = [super init];
gasGallons = 18;
oilQuarts = 8;
keys = 2;
return self;
Now my fordObject already has instance variables initialized when it is created. This then saves me the step of writing code in Main.m like [fordObject gasGallons: 18] and so on. Is this understanding right?
So in the end, I am getting 1 object back to use 'fordObject', with the instance variables already initialized to a value, right? Or, I read, and you guys have mentioned that id is a generic object. when -(id)init method is executed, a second object is created too, fordObject and idObject? In this case idObject would contain the initialized values and not fordObject?
Is id going to be it's own object or is it apart of fordObject?
Thanks again. I think I almost got this.
O' PS. I tried to dumb down my terminology to make it simple. I think it should have been [fordClass *exploreObject] and [fordClass* focusObject] and so on.