Methods such as these are called accessor methods. They, as the name implies, allow variables to be retrieved, and set - specifically, they are called "getters" and "setters".
The convention (which, you will however see in later chapters of the book is more than a convention) is to call the "getter" for a variable, for example, an NSString
called foo
- (NSString*)foo;
And the "setter":
- (void)setFoo:(NSString*)newFoo;
In the example above, the method is implemented to set the a new date value. Memory management is described in Chapter 4, but in short, the way Objective-C objects work is that they have a "retain count" - this is representitive of the number of references that the object has; when allocated, objects have a retain count of 1. The objects can then be sent the retain
or the release
message to increase or decrease the retain count respectively. A retain
message implies that the object sending the message wants to use the object, so retain
s it. A release
message implies that the object sending the message no longer wants to use the object, therefore decreasing the retain count. When the retain count of an object reaches 0, the object is deallocated. In this way, memory leaks are avoided.
The reason that date
is retained and entryDate
is released, is that date
is the new object that you want to "know about"; you are therefore claiming ownership of it by retain
ing it. The entryDate
variable points to the current date object, but since you are setting it to be a new value, you no longer need to know about it; therefore you release
it; this prevents a memory leak, since you will have originally retained this variable.
As I said before, once reading Chapter 4: Memory Management, the concept will become much more clear. For now, just accept it and understand the reasoning behind it when it is explained.