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:

@​p​r​o​p​e​r​t​y​ ​(​n​o​n​a​t​o​m​i​c​,​ ​c​o​p​y​)​ ...

Get Objective-C Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.