Changes

Jump to: navigation, search
no edit summary
== Data Encapsulation ==
In the previous chapter we mentioned briefly the concept of data encapsulation whereby data is contained within objects and is not accessible by any means other than via methods defined on the class. In our BankAccount class example we defined two instance variables and the then wrote some methods to get, set and display those values. In this chapter we will look at some alternate programming techniques to access the data encapsulated in a class.
== Synthesized Accessor Methods ==
Note that regardless of data types of the properties involved, the variable names may be placed on a single ''@synthesize'' line as illustrated above.
Having created the accessors, the question that now arises is what these methods are named. In assigning a name to a synthesized accessor methodsmethod, Objective-C takes the name of the instance variable (for example ''accountBalance''), capitalizes the first letter (''AccountBalance'') and then pre-fixes it with ''set'' (''setAccountBalance''). For example:
<pre>
== Direct Access to Encapsulated Data ==
Data encapsulation encourages the use of methods to get and set the values of instance variables in a class. Now is probably the time to point out this is a guideline rather than a rule. In fact, it is quite common for developers to want to directly access an instance variable without having to go through an accessor method. The Objective-C syntax for referencing an instance variable is as follows:
<tt>[''classinstance variablename'']</tt>
== Objective-C and Dot Notation ==
Those familiar with object oriented programming in Java, C++ or C# are probably reeling a little from the syntax used in Objective-C. They are probably thinking life was much easier when they could just use something called ''dot notation'' to set and get the values of instance variables. The good news is that one of the features introduction introduced into version 2.0 of Objective-C is support for dot notation.
Dot notation involves accessing an instance variable by specifying a class instance followed by a dot followed in turn by the name of the instance variable or property to be accessed:

Navigation menu