Sunday, August 28, 2011

Do you know Objective-c?: What is Cocoa?(II)

As we saw in the last post, then shows a video as a practical example, showing the structure that must follow to the design pattern MVC.


Because even I do not have audio on videos, as a clarification, the video content is summarized in the following diagram, where the project subdivide the three logical divisions that we saw in the MVC pattern.


-Belonging to the Model, Class CalculadoraBrain, will be declared the ivariables* and methods of operation (pulsed operation store, store digit, etc...).
-Belonging to Controller, CalculadoraViewController class, vriables are declared IB (Outlet and Action), which correspond to actions that may be given in the View, Model and methods that correspond performed at each time.
- Belonging to the View will generate the elements of the GUI of our application, linking these elements to events and actions to be taken for control

*ivariables: instance variables.


Thus, as we see, bind our three logical parts of our calculator, achieving independence and stability.

Friday, August 26, 2011

Do you know Objective-c?: What is Cocoa?

It's important to know what is Cocoa like concept and what involves. It is a basic concept to the start time to programming in Objective-C. Cocoa is, itself, a set of object-oriented frameworks that will allow the development of applications in different settings available in Mac OS X.
Once seen what is Cocoa, we know that, for developing applications with Cocoa, it is also necessary to know the concept of MVC (Model View Controller), which is nothing more than a modular design based pattern by which we can made to divide application logic into Model, View and Controller. Thus, the unit code in each design achieves stability and independence from the rest not being affected by changes on the code of other units.
The following are the characteristics of each of the modules of this pattern:

I) Model:
The model portion defines your application's underlying data enguine and is responsible for maintaining the integrity of data. Features:
  • It is usually a simple subclass of NSObject, or an instance of NSManagedObject to CoreData.
  • Contains a set of instance variables in which stores data.
  • Contains methods to access those instance variables.
  • Contains methods to generate object instances of other classes.
  • Contains a dealloc method (see its useful later).
  • It may contain methods for the modification of the internal object models.
  • It is reusable.
II) View:
The view portion defines the user interface for your application and has no explicit knowledge of the origin of the data displayed in that interface. Features:
  • Contains a NSView subclass.
  • Contains a drawRect method, which will form the basic of all methods of 'drawing'.
  • It can be a subclass, but rarely is the case.
  • Makes excessive use of Delegates for graphic.
  • It is reusable.
*Delegate: a delegate is a "connection" between objects, which allos the passage of messages in response to an event. We shall see later how to implement them in Cocoa.

III) Controller:
The controller portion acts as a bridge between the model and view and coordinates the passing of data between them. Features:
  • Intermediary between the view and model.
  • It is usually a subclass of NSObject.
  • Contains outlets and actions for IB.
  • Contains instance variables and collections to have and maintain model objects.
  • Available methods of handling and composition of model objects.
  • Contains the main method of awakeFromNib.*
*awakeFromNib: a method which comprises a protocol for recording information in the file window nib (NeXT Interface Builder). Classes can implement this method to initialize the state information after the objects have been loaded from the nib file.

Also, there is a dessing pattern called delegation, which is a way of modifying complex objects without subclassing them. Instead of subclassing, you use the complex object as is an object, which is referred to as the delegate object. At predefined times, the complex object then calls the methods of the delegate object to give it a chance to run its custom code.

Eveything has been explained in this post is resumed thanks to the following scheme



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.







Monday, August 22, 2011

First steps on Objective-C

After world of developing applications for mobile platforms Mac OSX called my attention, this blog turn up, where I will share my learning about Objective-C&Cocoa world and practical examples on the subject. Now, a week has passed since I got what it takes to star, and with the help of the documentation that Apple provides in his side http://developer.apple.com/, tutorials and other information circulating on the web, I mounted my fist App for Iphone, which is considered the advanced 'Hello world' in developing app for Smartphone. This is a simple calculator, with basic operations, with which I was able to refine the basic concepts and design patterns of App for Iphone.