Wednesday, May 5, 2010

Back to basics

# @interface declaration is identical to a regular function
# @implementation shows a new keyword: super

* Similar to Java, Objective-C only has one parent class.
* Accessing it's super constructor is done through [super init] and this is required for proper inheritance.
* This returns an instance which you assign to another new keyword, self. Self is similar to this in Java and C++.

# 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++. This is gotten from including NSObject.
# After you've initialized the varialbes, you return yourself with return self;
# The deafult constructor is -(id) init;
# Constructors in Objective-C are technically just "init" methods, they aren't a special construct like they are in C++ and Java.
+ is used. The + denotes a class level function.

Interface is a class which contain all unimplemented methods( can call them also abstract methods)
We will implement these methods in derived class of interface.
ie an interface is actually not inherited but implemented.
We cann't create an instance of interface object instead we can create an instance for derived class objects


classes in Objective-C provide the basic construct for encapsulating some data with the actions that operate on that data. An object is a runtime instance of a class,

Protocols and Delegates

Protocols and Delegates

A protocol declares methods that can be implemented by any class. Protocols are not classes themselves. They simply define an interface that other objects are responsible for implementing. When you implement the methods of a protocol in one of your classes, your class is said to conform to that protocol.

Protocols are used frequently to specify the interface for delegate objects. A delegate object is an object that acts on behalf of, or in coordination with, another object. The best way to look at the interplay between protocols, delegates, and other objects is to look at an example.

The UIApplication class implements the required behavior of an application. Instead of forcing you to subclass UIApplication to receive simple notifications about the current state of the application, the UIApplication class delivers those notifications by calling specific methods of its assigned delegate object. An object that implements the methods of the UIApplicationDelegate protocol can receive those notifications and provide an appropriate response.

The declaration of a protocol looks similar to that of a class interface, with the exceptions that protocols do not have a parent class and they do not define instance variables. The following example shows a simple protocol declaration with one method:

@protocol MyProtocol
- (void)myProtocolMethod;