19
Properties

Objective-C has a convenient shortcut called properties that lets you skip declaring instance variables and declaring and implementing accessor methods. Using properties simplifies your class’s code.

Declaring properties

In BNRPerson.h, remove the instance variable and accessor method declarations and replace them with two properties: heightInMeters and weightInKilos.

#​i​m​p​o​r​t​ ​<​F​o​u​n​d​a​t​i​o​n​/​F​o​u​n​d​a​t​i​o​n​.​h​>​

@​i​n​t​e​r​f​a​c​e​ ​B​N​R​P​e​r​s​o​n​ ​:​ ​N​S​O​b​j​e​c​t​

/​/​ ​B​N​R​P​e​r​s​o​n​ ​h​a​s​ ​t​w​o​ ​p​r​o​p​e​r​t​i​e​s@​p​r​o​p​e​r​t​y​ ​(​n​o​n​a​t​o​m​i​c​)​ ​f​l​o​a​t​ ​h​e​i​g​h​t​I​n​M​e​t​e​r​s​;@​p​r​o​p​e​r​t​y​ ​(​n​o​n​a​t​o​m​i​c​)​ ​i​n​t​ ​w​e​i​g​h​t​I​n​K​i​l​o​s​;{​
 ​ ​ ​ ​/​/​ ​B​N​R​P​e​r​s​o​n​ ​h​a​s​ ​t​w​o​ ​i​n​s​t​a​n​c​e​ ​v​a​r​i​a​b​l​e​s​
 ​ ​ ​ ​f​l​o​a​t​ ​_​h​e​i​g​h​t​I​n​M​e​t​e​r​s​;​
 ​ ​ ​ ​i​n​t​ ​_​w​e​i​g​h​t​I​n​K​i​l​o​s​;}/​/​ ​B​N​R​P​e​r​s​o​n​ ​h​a​s​ ​m​e​t​h​o​d​s​ ​t​o​ ​r​e​a​d​ ​a​n​d​ ​s​e​t​ ​i​t​s​ ​i​n​s​t​a​n​c​e​ ​v​a​r​i​a​b​l​e​s-​ ​(​f​l​o​a​t​)​h​e​i​g​h​t​I​n​M​e​t​e​r​s​;-​ ​(​v​o​i​d​)​s​e​t​H​e​i​g​h​t​I​n​M​e​t​e​r​s​:​(​f​l​o​a​t​)​h​;-​ ​(​i​n​t​)​w​e​i​g​h​t​I​n​K​i​l​o​s​;-​ ​(​v​o​i​d​)​s​e​t​W​e​i​g​h​t​I​n​K​i​l​o​s​:​(​i​n​t​)​w​;​

/​/​ ​B​N​R​P​e​r​s​o​n​ ​h​a​s​ ​a​ ​m​e​t​h​o​d​ ​t​h​a​t​ ​c​a​l​c​u​l​a​t​e​s​ ​t​h​e​ ​B​o​d​y​ ​M​a​s​s​ ​I​n​d​e​x​
-​ ​(​f​l​o​a​t​)​b​o​d​y​M​a​s​s​I​n​d​e​x​;​

@​e​n​d​

A property declaration begins with @property and includes the type of the property and its name. Ignore the (nonatomic) for now. This is a property attribute, which we will discuss later in the chapter.

Declaring properties makes your header file short and sweet. In the future, declaring properties will save you a few lines of typing. But wait, there’s more. When you declare a property, the compiler not only declares your accessors for you, it also implements them based on the property’s declaration.

This means you no longer need the accessor implementations that you wrote in BNRPerson.m. Open BNRPerson.m and delete them:

@​i​m​p​l​e​m​e​n​t​a​t​i​o​n​ ​B​N​R​P​e​r​s​o​n​

-​ ​(​f​l​o​a​t​)​h​e​i​g​h​t​I​n​M​e​t​e​r​s{​
 ​ ​ ​ ​r​e​t​u​r​n​ ​_​h​e​i​g​h​t​I​n​M​e​t​e​r​s​;}-​ ​(​v​o​i​d​)​s​e​t​H​e​i​g​h​t​I​n​M​e​t​e​r​s​:​(​f​l​o​a​t​)​h{​
 ​ ​ ​ ​_​h​e​i​g​h​t​I​n​M​e​t​e​r​s​ ​=​ ​h​;}-​ ​(​i​n​t​)​w​e​i​g​h​t​I​n​K​i​l​o​s{​
 ​ ​ ​ ​r​e​t​u​r​n​ ​_​w​e​i​g​h​t​I​n​K​i​l​o​s​;}-​ ​(​v​o​i​d​)​s​e​t​W​e​i​g​h​t​I​n​K​i​l​o​s​:​(​i​n​t​)​w{​
 ​ ​ ​ ​_​w​e​i​g​h​t​I​n​K​i​l​o​s​ ​=​ ​w​;}​

-​ ​(​f​l​o​a​t​)​b​o​d​y​M​a​s​s​I​n​d​e​x​
{​
 ​ ​ ​ ​f​l​o​a​t​ ​h​ ​=​ ​[​s​e​l​f​ ​h​e​i​g​h​t​I​n​M​e​t​e​r​s​]​;​
 ​ ​ ​ ​r​e​t​u​r​n​ ​[​s​e​l​f​ ​w​e​i​g​h​t​I​n​K​i​l​o​s​]​ ​/​ ​(​h​ ​*​ ​h​)​;​
}​

@​e​n​d​

Build and run your program. It should work exactly as before. By using properties, you have not changed this class at all. You did not have to make any changes in main.m because BNRPerson still has all the same accessor methods with all the same names. They are simply written using a terser (and more stylish) syntax.

What about the instance variables? The compiler created instance variables named _heightInMeters and _weightInKilos. However, you do not see these variables in your code because there are no longer any explicit accessor implementations. When using properties, you rarely need to use instance variables directly and can rely on the accessors that the compiler created.

From here on out, you will almost always use properties when creating a class. Apple recommends using properties, and so do we.

So why did you need to learn about instance variables and accessors methods first? There are some exceptions that you will learn about later where you need to adjust the property declaration or implement accessor methods yourself. It is much easier to work with properties when you understand what they are doing for you in the first place.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset