Wednesday, August 24, 2011

Do you know Objective-C? Start here

For those seeking a formal presentation of this programming language, can refer to http://es.wikipedia.org/wiki/Objective-C. For those who want to know the basic concepts, I can say that Objective-C is a programming language, currently used for programming on Mac OS X (iPhone,iPod,iMac), based on object orientation and regarded as a 'hybrid' between languages as well known as Java and C++.
Knowing this, we'll see basic concepts of programming in Objective-C to be compared perhaps with a language closer, such as Java and C++.

I) Classes
Classes in Objective-C are similar to those generated in C++, separating interface from implementation, thus disposing of headers methods and variables in a file, and in another implementation. In Objective-C interface file will have .h extension and implementation file will have .m extension.
Before meeting any class, is essential to understand the concept of IOBOutlet and IBAction. Both are macros defined to refer to variables and methods (respectively), which can be used in Xcode Interface Builder, that is, the way we connect visual elements with the functionality of the application. Some of these variables can refer to objects like buttons, tables, labels, switches, etc...


II) Methods
To tell if a method is a class method or instance, a particular symbol will be used in the method header.
+: This is a class method (access to variables of the class).
- : This is an instance method (access to variables with value determined for each class instance).

Examples:

- (void) updateCoordinateX(int coord_x);        //Objective-C
public void updateCoordinateX(int coord_x);  //Java
__________________________________________________________________________________

- (void) updateCoordinateX: (int) coord_x Y: (int) coord_y;   //Objective-C
public void updateCoordinatesXY(int coord_x, int coord_y);  //Java
__________________________________________________________________________________

Using methods:
[satellite updateCoordinateX: nuevaCoordenadaX Y:nuevaCoordenadaY];  //Objective-C
satellite.updateCoordinateXY(nuevaCoordenadaX, nuevaCoordenadaY);   //Java
__________________________________________________________________________________

Nesting methods:
Satellite sat_1;                       //Satellite instantiate class object
sat_1= [[Satellite alloc]init];    //Reserve memory with alloc method and initialize the object attributes

II.I) Memory management methods:

An important point to note here, learning basic concepts of Objective-C, is that unlike Java, does not have a garbage collector that frees unused memory resources, so we must manage ourselves the process of allocating and freeing memory. To do this we have methods like ALLOCINIT and RELEASE.
  • ALLOC: Allocate memory for a particular object class that invokes it.
  • INIT: Initialization method attributes.
  • RELEASE: Release memory allocated to a class object.
With these basic concepts of Objective-C, and knowledge about the management of Xcode, we can develop a basic 'Hello World' as it shown in the video below.







No comments: