Objective-C

This blog post will introduce you to the concepts of iPhone App Development and Objective-C Programming. The simplicity and elegance of Apple's products is attracting many people to buy their products. And then successful programmers can become successful entrepreneurs if they could develop market-targeted apps.
The concept of MVC (Model-View-Controller) and the flexibility of programming for multiple devices (iPhone, iPad, Mac) just with Objective-C has given an opportunity to many programmers to expose their programming knowledge and contribute valuable apps to human society!


Basics of Object-oriented programming in Objective-C


Basic variable declaration:

int number = 1;
long long int number = 1;
const char myconst = ‘c’;

Datatypes & Format Specifiers:

Syntax for declaring an objective-c class interface:

@interface NewClassName: ParentClass {
ClassMembers;
}
ClassMethods;
@end

Example interface:
@interface BankAccount: NSObject {
…}
@end

Adding instance variable to a Class:

@interface BankAccount: NSObject
{
double accountBalance;
long accountNumber;
}
@end

Define class methods:

  • Class Methods: preceded by ‘+'
  • Instance Methods: preceded by ‘-‘
-(void) setAccountNumber: (long) y;
-(long) getAccountNumber;
-(void) setAccount: (long) y andBalance: (double) x; //Multiple parameters

Declaring the methods of a Objective-C class(Blueprint):

@interface BankAccount: NSObject
{
double accountBalance;
long accountNumber;
}
-(void) setAccountNumber: (long) y;
-(long) getAccountNumber;
-(void) setAccount: (long) y andBalance: (double) x;
@end

Syntax for Objective-C Class Implementation:

@implementation NewClassName
classMethods
@end

Defining the methods of a Objective-C Class:

@implementation BankAccount
-(void) setAccount: (long) y andBalance: (double) x
{
accountBalance = x;
accountNumber = y;
}
-(long) getAccountNumber
{
return accountNumber;
}

Declaring and initializing a class instance:

BankAccount *account1;
account1= [BankAccount alloc]; //allocate memory
account1= [BankAccount init]; //initialize the instance

This is equivalent to:
BankAccount *account1= [[BankAccount alloc] init];

How to access instance variables and methods:

  • account1.accountBalance = 1000.00; //Instance Variable
  • [account1 setAccount: 19986576 : 1000.00]; //Instance Method
  • long theAccountNumberIs = [account1 getAccountNumber]; //Instance Method

Log messages for debugging:

NSLog(@“Account Number %li has a balance of %f”, accountNumber, accountBalance);

No comments:

Post a Comment