Properties

Each time we’ve added an instance variable to BNRItem, we’ve declared and implemented a pair of accessor methods. Now we’re going to see how to use properties instead. Properties are a convenient alternative to writing out accessors for instance variables – one that saves a lot of typing and makes your class files much clearer to read.

Declaring properties

A property is declared in the interface of a class where methods are declared. A property declaration has the following form:

@​p​r​o​p​e​r​t​y​ ​N​S​S​t​r​i​n​g​ ​*​i​t​e​m​N​a​m​e​;​

When you declare a property, you are implicitly declaring a setter and a getter for the instance variable of the same name. So the above line of code is equivalent to the following:

-​ ​(​v​o​i​d​)​s​e​t​I​t​e​m​N​a​m​e​:​(​N​S​S​t​r​i​n​g​ ...

Get iOS 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.