Class Properties
I’ve waited to describe properties until now because I wanted you to
have a good working knowledge of instance variables and companion getter
and setter methods in their traditional sense. Introduced to the
Objective-C language as recently as 2007, properties provide a compact (in
terms of source code) way of writing getters and setters (collectively known as
accessor methods) for your instance variables. Of
interest to those with JavaScript expertise, properties allow Objective-C
statements to get and set instance variable values through the same dot
syntax (objectName
.
propertyName
) that
is so automatic to you by now. On the one hand, the syntax might feel
comfortable to you, but on the other hand, it adds to what sometimes feels
like “syntax soup” in writing iOS SDK app code (old C constructs, original
Objective-C syntax, and JavaScript-like Objective-C
expressions).
Creating properties is a two-step process. I’ll treat each step separately.
Specifying Properties in the Header File
Because properties are there to help build getter and setter
methods behind the scenes, you must still declare your instance
variables in the @interface
section
of the header file as before. Nothing changes there.
Property declarations go below the curly brace section of the header, where your instance methods (if any) also go. Even though properties feel like they’re instance variables, they are in truth specifications for getter and setter methods—thus they are defined where other object ...
Get Learning the iOS 4 SDK for JavaScript Programmers 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.