Properties - FREE Guru Level Training For Beginners (2014)

FREE Guru Level Training For Beginners (2014)

Chapter 6: Properties

“The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct.”- Donald Ervin Knuth

Objects consist of binding data. In addition to this they also consist of information (or functionality) that define how the data must be used or processed. This data is called the property of the object. So, in simpler words, the property of the object will define how the data will be processed.

Two methods are defined in every property. One of these is used to assign a value to the property and this is called the setter. Then, there’s the getter which will get the value of the property. So, if an integer is set as the value for the property, the setter is used to assign the value to the property. The setter will evaluate if the value assigned is correct and is within a certain range. If it is, then, this value will be passed on to the getter.

The values of an object are encapsulated by properties in Objective C. An object must know its task so it can perform an action. The value of the object may be anything. It can be a number, person, animal or anything else of the sort. Some objects may contain more than one value too.

One must define the information in the property so that it is visible to other classes. This also makes the property accessible to other classes. Look at the following example to understand how one can declare a property:

@interface THISperson : NSObject

@property NSString *johnsAge;

@property NSString *johnsHeight;


In the example given above THISperson is a class which holds the string’s properties. John and Doe are the string’s properties. In this case, we are aware of the object’s behavior. I have accessed the object properties because of the behavior it has shown rather than doing so directly.

Two things are required here: the class variable and the @property directive to define the property itself. Look at the code again and you’ll find “NSString” written right after @property in the code. This is called the property attribute and it defines how the property will behave. Since it has been stated that johnsAge and johnsHeight are NSStrings, the properties behave/act like NSStrings.

You have the option of either setting the property values or you can use accessor methods to do the same.

NSString *johnsAge = [unknownAge johnsAge];

[unknownAge setjohnsAge:@"25"]

JohnsAge is the getter here because it gets the value of the property (which in this case is johnsAge). The setter in this case is setjohnsAge.

At this point you can add in an attribute as follows:

@property (readonly) NSString *unknownAge;

The attribute in this case is readonly and what it does is that it enables the compiler to synthesize the accessor methods. So, unknowAge is the getter method that is synthesized by the readonly attribute. Bear in mind that this is the getter and so, it cannot be used to set John’s age because it cannot act as a setter.


This chapter tells you about properties in iOS7. Basically, properties are used to define how data encapsulated by the objects will be processed. There are two types of methods that determine this- the setter and the getter method. The former is used to assign a value to the object. This is then, passed to the getter after it is accepted by the setter. Also, attributes are used to determine how the property will behave. In the example given above it was specified that it will merely act as an NSString but you can also use attributes like readwrite, readonly and such like.