Objective-C : Data
we learn how to manage memory on object creation : Objective-C : Memory management methods
Objective-C programs are composed of the following two fundamental elements:
·Program statements (code): This is the
part of a program that performs actions and they are called methods.
·Program data: The data is the information
of the program which is affected by the program functions.
is an Object-Oriented Programming concept that binds together the data and
functions that manipulate the data and that keeps both safe from outside
interference and misuse. Data encapsulation led to the important OOP concept of
encapsulation is a mechanism of bundling the data and the functions that
use them, and data abstraction is a mechanism of exposing only the
interfaces and hiding the implementation details from the user.
is an important concept in object-oriented programming. By using class methods
to access data stored in object properties, we can separate how that data is
returned from how it is stored internally.
important because the structure of the data and how it is formatted can and
will change over time. By encapsulating the data using class methods, we
protect ourselves from change by making sure the data returned is always
consistent. If we access the object properties directly, we would need to go
through our code and make changes every time the structure or format of the
Objective-C, we use properties, and the accessor methods in particular, help
to hide the instance variables themselves. By indirectly accessing them, you
get to control their access. You can also force the issue by making them readonly, which means only the
containing class can use them. By using methods, you also don’t have to worry
about how the instance variables themselves are set up; you simply access the
methods that are provided, and have confidence the data returned will always be
returned the same way, regardless how it is stored or structured.
The code for
the example Bank application, after being adapted to use properties and synthesized accessors, now reads as follows:
@interface BankAccount : NSObject@property double accountBalance;
@property long accountNumber;
-(void) setAccount: (long) y andBalance: (double) x;-(void) displayAccountInfo;
-(void) setAccount: (long) y andBalance: (double) x;
_accountBalance = x;
_accountNumber = y;
NSLog (@"Account Number %ld has a balance of %f", _accountNumber, _accountBalance);
int main(int argc, const char * argv)
account1 = [BankAccount alloc];
account1 = [account1 init];
[account1 setAccountBalance: 1500.53];
[account1 setAccountNumber: 34543212];
[account1 setAccount: 4543455 andBalance: 3010.10];
NSLog(@"Number = %ld, Balance = %f", [account1 accountNumber], [account1 accountBalance]);
power of properties is most noticeable in the BankAccount.m
implementation file where the use of properties has reduced the number of
methods that needed to be implemented from six down to two.
Next, we will learn about : Objective-C : Categories