Starting with Objective C

Basics of Objective C Objective-C is a reflective, object-oriented programming language that adds Small talk-style messaging to the C programming language. Objective-C is a strict superset of C; it is possible to compile any C program with an Objective-C compiler, and to freely include C code within an Objective-C class. Creating classes Objective-C requires that the interface and implementation of a class be in separately declared code blocks. By convention, we place the interface in a header file and the implementation in a code file. The header files, normally suffixed .h, are similar to C header files while the implementation (method) files, normally suffixed .m, can be very similar to C code files. Interfaces The interface of a class is usually defined in a header file. A common convention is to name the header file after the name of the class, e.g. Fraction.h would contain the interface for the class Fraction. Fraction.h @interface Fraction: NSObject { int numerator; int denominator; } //multiple parameters first parameter is n and second parameter is d and Deno is named argument. -(void) setNumerator:(int) n Deno:(int) d; -(void) print;                         // no parameter -(void) setNumerator:(int) d;             // single parameter d is only parameter @end
      • Inheritance is specified as Class: Parent, as seen with Fraction: NSObject.
      • Instance variables go between @interface Class: Parent { …. }
      • Instance methods follow after the member variables. The format is:
      • scope (returnType) method Name: (parameter1Type) parameter1Name; scope  refers to  class or instance. instance methods begin with – , class level methods begin with +
      • Interface ends with @end.
Implementation The actual code is written in the implementation file. “@implementation ClassName” starts the implementation “@end” ends it. Fraction.m @implementation Fraction -(return type)methodname: (parameter1type)  parameter1name{ …….// code to be written } @end  Once the class is written, it can be instantiated. Example shows how to do this: Fraction *frac = [[Fraction alloc] init];  //instantiation In this Fraction is a class. Memory is allocated by alloc and then initialized by init.  Access privileges  The default access is @protected. But it has public/private/protected access.  Public variable can be accessed outside the class but private variable can be accessed within the the class only. Following example shows access: Access.h  #import <Foundation/NSObject.h> @interface Access: NSObject { @public int publicVar; @private int privateVar; } @end  Inheritance  If we have already some class we can inherit it in other class. <import number.h> @interface fraction: number  //class fraction inherits class number { } Now by inheriting the class we can use the methods of class number in class fraction. Also we can access Protocols Objective-C was extended at NeXT to introduce the concept of multiple inheritance of specification, but not implementation, through the introduction of protocols. Locking.h @protocol locking -(void) lock; -(void) unlock; @end @interface SomeClass : SomeSuperClass <Locking> @end Constructors Constructor is defined as method having same name as class. In objective c we can define constructor as follow: Fraction.h -(Fraction*) Numerator: (int) n Fraction.m (Fraction*) Numerator: (int) n { self = [super init]; if ( self ) { //code to be written } } Accessing it’s super constructor is done through [super init] and this is required for proper inheritance. If ( self ) is the same as if ( self != nil ) to make sure that the super constructor successfully returned a new object. nil is Objective-C’s form of NULL from C/C++. The default constructor is -(id) init.  Memory management: Retain and Release #import “Fraction.h” #import <stdio.h> int main( int argc, const char *argv[] ) { Fraction *frac1 = [[Fraction alloc] init]; Fraction *frac2 = [[Fraction alloc] init]; // print current counts printf( “Fraction 1 retain count: %i\n”, [frac1 retainCount] ); printf( “Fraction 2 retain count: %i\n”, [frac2 retainCount] ); // increment them [frac1 retain]; // 2 [frac2 retain]; // 2 // print current counts printf( “Fraction 1 retain count: %i\n”, [frac1 retainCount] ); printf( “Fraction 2 retain count: %i\n”, [frac2 retainCount] ); // decrement [frac1 release]; // 2 [frac2 release]; // 1 // print current counts printf( “Fraction 1 retain count: %i\n”, [frac1 retainCount] ); printf( “Fraction 2 retain count: %i\n”, [frac2 retainCount] );

// release them until they dealloc themselves [frac1 release]; // 0 [frac2 release]; // 0 [super dealloc] } Initially, count is 1 The retain calls increment the counter.The release calls decrement it.One can get the count as an int by calling [obj retain Count].Once the retain Count reaches 0, both objects dealloc themselves.

Dealloc When your object contains other objects, you must free them whenever you yourself dealloc. One of the nice advantages to Objective-C is you can pass messages to nil, so there isn’t a lot of error checking to release an object. In above example we have written [super dealloc] in the end.