stripeColor property release/retain handling


I wanted to point out something that I found confusing and would have liked a better explanation.

In the ‘Detecting Shakes’ section you define a stripeColor instance var and then release/retain when you set the var.

This was confusing at first because the approach taken up to that point in the book was to create and synthesize a property (nonatomic, retain) and then use the setter method [self setStripeColor:…] which would automatically handle the release/retain.

I think it would be more clear to:

  1. Take the approach used up to that point in the book.
  2. Take this different approach and explain that since you didn’t synthesize a property that you need to manually handle the retain/release since there’s no setter to automatically handle it.

Excellent book by the way!


There is a distinct difference between properties and instance variables which is difficult to understand early on:

Instance variables are the information an instance of a class has. Values, pointers to other objects, buffers of information, etc. The values of an object’s instance variables are what define it - for example, a Possession instance has a serial number, name and so on. By default, an object is the only thing that can access its own instance variables.

Sometimes, we would like to make accessor methods for the instance variables of a class. We do this because we want other objects to have the ability to get or set that instance variable. For example, if (and you will in later chapters) you want a controller object to change the name of a Possession instance because the user used a text field to change that value, that controller object can grab the string from the text field and use the accessor of the Possession instance to set the name instance variable with that string.

Properties are simply a shortcut that define accessor methods (and synthesizing them creates those methods as you have defined). You will only use properties if you want accessor methods and you only want accessor methods if you want other objects to access an instance variable.

In this case, you don’t want other objects accessing the stripeColor of the view. You may, especially for the challenge, change your mind about that. But for right now, this information is private to the HypnosisView. This actually happens fairly often and in some cases is necessary.

But yes, in cases like this, you will have to “manually” handle the retain and release of the instance variable.