In preparation for a forthcoming blogpost on self, super, polymorphism and inheritance in Xcode and iOS5, which has been inspired and informed by Matt Neuburg's book Programming iOS 5, I'm posting some sample code to illustrate the points that will be made. If you wish to experiment, then copy and paste this into your Xcode project.
When you are experimenting with methods and code in Xcode you will find yourself inside the ViewController.m file sending messages to self as a way of running your code from viewDidLoad, for example, and this can lead to confusion about what self really is and you can find yourself slipping into thinking of self as the file within which you are writing the code when in actual fact it is the instance of the object.
To explain this let's take a look at the viewDidLoad method of our ViewController.m file:
If we'd wanted to show the change more clearly we could have used a button (see this earlier post) but this isn't the thing in question here. We can trust that the square was yellow before it was blue even if it was only for an instant and we didn't see the change because it happened too quickly.
When you are experimenting with methods and code in Xcode you will find yourself inside the ViewController.m file sending messages to self as a way of running your code from viewDidLoad, for example, and this can lead to confusion about what self really is and you can find yourself slipping into thinking of self as the file within which you are writing the code when in actual fact it is the instance of the object.
To explain this let's take a look at the viewDidLoad method of our ViewController.m file:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
SquareView *squareViewOne = [[SquareView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
[self.view addSubview:squareViewOne];
squareViewOne.backgroundColor=[UIColor yellowColor];
SquareView *squareViewTwo = [[SquareView alloc] initWithFrame:CGRectMake(500, 500, 100, 100)];
[self.view addSubview:squareViewTwo];
squareViewTwo.backgroundColor=[UIColor yellowColor];
[squareViewOne backgroundColorChange];
}
Now let's imagine that our corresponding method in SquareView.m looks like this:
-(void)backgroundColorChange {
[self setBackgroundColor:[UIColor blueColor]];
}
What happens when our view loads and our viewDidLoad method is called? First of all two instances of SquareView are created and the background colour of both is set to yellow separately. The method backgroundColorChange is then sent to the first instance. As you will see if you run this code, only that instance changes colour. The second instance is left alone, as is the top level view of our view controller.
This demonstrates that it is the class instance (i.e. object) that is self, just as when we send a message to self to run a method within our view controller it is the instance we are within that is being sent the message.
If we'd wanted to show the change more clearly we could have used a button (see this earlier post) but this isn't the thing in question here. We can trust that the square was yellow before it was blue even if it was only for an instant and we didn't see the change because it happened too quickly.
(Note: If you've followed previous posts in this series then the files shouldn't be too difficult to imagine in their entirety based on these two pieces of code.)
Comments
Post a Comment