Implementing accessor methods
By default, the compiler synthesizes accessor methods for any property you declare. Usually, accessor method implementations are straightforward and thus well-suited to being handed off to the compiler.
However, there are times where you will need an accessor to do something out of the ordinary. When this is the case, you can implement the accessor yourself in the implementation file.
There are two reasonable cases to implement an accessor yourself:
-
You need to update the app’s user interface when the change occurs.
-
You need to update some cached info when the change occurs.
For example, say you declared a property in a header file:
@property (nonatomic, copy) NSString* currentState;
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access