Introduction to objective c

29
Introduction to Objective C

description

 

Transcript of Introduction to objective c

Page 1: Introduction to objective c

Introduction to Objective C

Page 2: Introduction to objective c

Objective C •  Objective C is a programming language, which is used by Apple for

developing the application for iPhone and Mac Systems. •  Objective C is very old programming language and it was designed and

developed in 1986. Now Objective C has become popular once again as it is being used by Apple to develop applications for Mac system and iPhone.

•  Full superset of C language •  Allows any traditional C code.

•  Adds powerful Object oriented capabilities.

Page 3: Introduction to objective c

OverView •  Objective C consists of objects, classes, instance variables,

methods. •  Built entirely around objects. •  Objects like Windows, views, buttons, controllers exchange

information with each other, respond to events, pass actions to run a program.

•  In C we write .c and .h files, here we write .h and .m files. •  .h are the header files and .m are the source code or implementation

files.

Page 4: Introduction to objective c

Objective C Language •  Keywords •  Message •  Classes and method declaration •  Instance Methods and Class Methods •  Constructors •  User Defined Constructors •  Categories •  Protocols

Page 5: Introduction to objective c

Keywords

Keyword Definition

@interface This is used to declare a class/interface

@implementation This is used to define class/category

@protocol This is used to declare a protocol

Keywords in objective C has a prefix @ appended to them. We will look at the keywords used for different purposes in this section

Page 6: Introduction to objective c

Interface The declaration of a class interface begins with the compiler directive

@interface and ends with the directive @end.

@interface ClassName : ItsSuperclass {

instance variable declarations } method declarations @end

the name of the interface file usually has the .h extension typical of header files.

Keywords Cont…

Page 7: Introduction to objective c

Implementation

The definition of a class is structured very much like its declaration. It begins with the @implementation directive and ends with the @end directive @implementation ClassName : ItsSuperclass method definitions @end

The name of the implementation file has the .m extension, indicating that it contains Objective-C source code.

Keywords Cont…

Page 8: Introduction to objective c

Keywords cont..

Keyword Definition

@private The instance variable is accessible only within the class that declares it.

@public The instance variable is accessible everywhere

@protected The instance variable is accessible within the class that declares it and within classes that inherit it.

Next are the access modifiers. They decide the visibility/ scope of the instance variables/methods

Page 9: Introduction to objective c

Other keywords:

Keyword Description

@class Declares the names of classes defined elsewhere.

@”string” Defines a constant NSString object in the current module and initializes the object with the specified string.

@property Provides additional information about how the accessor methods are implemented

@synthesize Tells the compiler to create the access or method(s)

Keywords Cont…

Page 10: Introduction to objective c

Declaring a simple property @interface MyClass : NSObject {

float value; } @property float value; @end A property declaration is equivalent to declaring two accessor methods i.e. -(float)value; -(void)setValue:(float)newValue; These methods are not shown in the code but can be overridden.

Keywords Cont…

Page 11: Introduction to objective c

Synthesizing a property with @synthesize @implementation MyClass : NSObject @synthesize value; @end When a property is synthesized two accessor methods are generated i.e. -(float)value; -(void)setValue:(float)newValue; These methods are not shown in the code but they can be overridden.

Keywords Cont…

Page 12: Introduction to objective c

Self

l  Self is a keyword which refers to current class.

{ [self setOrigin:someX :someY]; }

In the example above, it would begin with the class of the object receiving

the reposition message.

Keywords Cont…

Page 13: Introduction to objective c

super

l  It begins in the superclass of the class that defines the method where super appears.

l  Super is a keyword which refers to the parent class.

{ [super init]; }

{ [super dealloc]; } In the example above, it would begin with the superclass of the class

where reposition is defined.

Keywords Cont…

Page 14: Introduction to objective c

Message •  It’s the most important extension to C •  Message is sent when one object asks another to perform a specific action. •  Equivalent to procedural call in C •  Simple message call looks like [receiver action], here we are asking the

receiver to perform the action. •  Receiver can be a object or any expression that evaluates to an object. •  Action is the name of the method + any arguments passed to it.

Page 15: Introduction to objective c

Message with Arguments •  Sometimes we pass one or more arguments along with the action to the

receiver. •  We add a argument by adding a colon and the argument after the action like

[receiver action: argument] •  Real world example of this is [label setText:@”This is my button”]; •  String in Objective C is defined as @””; •  Multiple arguments can be passed to a action like this

[receiver withAction1:argument1 withacction2:argument2];

For example: [button setTitle:@”OK” forState:NO];

Page 16: Introduction to objective c

Classes and Method Declaration •  Class in objective C is a combination of two files ie .h and .m •  .h file contains the interface of the class. •  .m contains the implementation

@interface

Variable and methods declaration

@implementation

Method definitions

.h file .m file

Class Definition

Page 17: Introduction to objective c

Classes and Method Declaration •  Example of a Person class. •  Here we define the interface and implementation in Person.h and Person.m

file respectively Person.h file

#import<Foundation/NSObject.h> @interface Person: NSObject { NSString *name; } -(void) setName: (NSString *)str; +(void) printCompanyName; @end

Page 18: Introduction to objective c

Classes and Method Declaration •  Now the contents of Person.m file •  #import Person.h

@implementation Person -(void) setName: (NSString *) str { name=str; } +(void) printCompanyName { printf(“This is class method”); }

@end;

Here we have defined a Class Person which has a instance variable “name” and a method “setName”.

Page 19: Introduction to objective c

Classes and Method Declaration •  Using the Person class #import<stdio.h> #import “Person.m" int main() {

Person *c = [[Person alloc] init]; // Allocating and initializing Person [c setName : @”Rahul”]; // Setting Name of the allocated person [Person printCompanyName] // calls class method [c release]; // releasing the person object created return 1; // return

}

Page 20: Introduction to objective c

Instance and Class Methods •  In objective C we can define methods at two levels ie Class Level and

Instance level •  In previous Example we declared a method with a – sign prefixed. That was

a instance level method. •  If we put + instead of – then we get a class level method. •  A instance method can be called by the instances of the class. But a class

level can be called without creating any instance. •  Example to call a instance method;

Person *p=[[Person alloc] init]; [p setName:@”Sunil”];

•  Example to call class method [Person printCompanyName];

Page 21: Introduction to objective c

Creating multi parameter method Objective-C enables programmer to use method with multiple parameter. These parameter can be of same type or of different type. MyClass.h #import<Foundation/NSObject.h> @interface MyClass:NSObject{ } // declare method for more than one parameter -(int) sum: (int) a andb: (int) b andc:(int)c; @end

MyClass.m #import<stdio.h> #import"MyClass.h" @implementation MyClass -(int) sum: (int) a andb: (int) b andc:(int)c;{ return a+b+c; } @end

Page 22: Introduction to objective c

Creating multi parameter method Objective-C enables programmer to use method with multiple parameter. These parameter can be of same type or of different type. main.m #import"MyClass.m" int main() { MyClass *class = [[MyClass alloc]init]; NSLog(@"Sum is : %d",[class sum : 5 andb : 6 andc:10]); [class release]; return ; } Output: Sum is: 21

Page 23: Introduction to objective c

Constructors

•  When a class is instantiated a constructor is called which is used to initialize the object properties

•  When a constructor is called it returns an object of a class. •  If a user does not provide a constructor for a class the default one is used. •  The default constructor is

-(id) init; id is a special keyword in Objective C which can be used to refer to any object.

•  Remember in our Person class example while instantiating the Person class we called the constructor. [[Person alloc] init]; It returns a person object.

Page 24: Introduction to objective c

Categories

•  Typically when a programmer wants to extend the functionality of a class, he subclasses it and adds methods to it.

•  Categories can be used to add method to a class without

subclassing. •  Here’s how you create a category

@interface PersonCategory (personcat)

@implementation PersonCategory (personcat)

Page 25: Introduction to objective c

Categories

•  Implementation of category. •  personcat.h file contains •  #import “Person.h"

@interface Person (personcat) -(void) updateName: (NSString *) str; @end

personcat.m file contains

#import “personcat.h ” @implementation Person (personcat) -(void) updateName: (int)value{ Printf(“%d”,value); } @end

The updateName name method now behaves as if it’s the part of Person Class.

Page 26: Introduction to objective c

Protocols

•  Protocols are like interfaces in Java •  It declares a set of methods, listing their arguments and return types •  Now a class can state that its using a protocol in @interface statements in .h

file •  For example

@interface Person:NSObject <human> Here human is a protocol.

•  Defining a protocol

@protocol human <NSObject> -(void) eat; @end

Page 27: Introduction to objective c

Keywords Cont… •  Memory management keywords

Keyword Description

Alloc Allocates memory for an object

Retain Retains a object

Releae Releases memory of an object

Auto release Auto release memory used by an object

Page 28: Introduction to objective c

Memory Management

•  In objective C a programmer has to manage memory ie allocate and deallocate memory for objects.

•  While instantiating a person object we allocated the memory for the object

by this call. Person *p=[[Person alloc] init];

•  We have to release whatever objects we create programatically. Memory

management for other objects is taken care of by the Objective C runtime. •  We use release action to release the unused memory.

The syntax for this is [p release];

Page 29: Introduction to objective c

Thanks