miércoles, 7 de diciembre de 2011

Basic language syntax


Objective-C language can be a strange and uncomfortable digging into this technology. At first it may be awkward syntax, but once used to call methods, statements, and other memory management, things get easier.


Classes


Classes in Objective-C are composed of an interface or protocol (".h" extension), and an implementation (".m" extension)


Protocols


A protocol is a public contract that lists the statements of the methods and properties, in turn the protocol defines which is the parent class that inherits from.


What does this mean? All classes that use the class created will have knowledge of what properties and methods can be used, as well as who inherits the class.


An example:

  1. #import <UIKit/UIKit.h>
  2. @interface iPhoneTestViewController: UIViewController <UITableViewDelegate,
  3. UITableViewDataSource>
  4. {  
  5. }

In the top line as we can see the interface iPhoneTestViewController inherits from UIViewController.
Later I will explain in detail the utility of each Controller in Cocoa.


You can also see that in "<>" possibility of adding delegates that will
observe the events dispatched by the controller.
You can also import other interfaces of other classes to be used in it,
in this case we are importing UIKit, library required for all iPhone applications.

Implementations



Just as we saw that there are protocols that define how you will involve the class, these are
accompanied by a file that defines the implementation of it (. m).
This can be used to develop the behavior of the methods, overwrite
methods of the parent class and implement the event behavior of some
delegate. An example might be

  1. #import "iPhoneTestViewController.h"      //This is the header   
  2. @implementation iPhoneTestViewController
  3. //Class constructor
  4. - (id)init {    
  5.     [super init];
  6.     return self;
  7. }


This is a simple example of a class implementation, where the protocol is imported and we have an init method. The init method is used as a constructor of a class to instantiate,
for now I will not go into detail on class constructors or overloads.



No hay comentarios:

Publicar un comentario