sábado, 21 de dezembro de 2013

Methods in Objective-C

Write in english is not so good to me because my sense of humor will be lost... :)
But, when we write in english everybody believes. Then, I can put here a lot of lies and nobody will make a question.

Lets start... Today my post is about how call methods in Objective-C. The syntax is a little different from Java, that use a dot notation, like:

   myObject.myMethod(list of parameters)

In Objective-C we use [ ] (brackets) to call methods, like this:

   [myObject myMethod]

Is very common to see code with two or more brackets because we are trying to get an object and then call a method of this object. In the next code, [myObject getAnObject] returns an object, then I call otherMethod from this object:

[[myObject getAnObject] otherMethod]

Lets see an example. I modeled a SpaceCraft' class, and the code (.m and .h) is showed below:

#import <Foundation/Foundation.h>

@interface SpaceCraft : NSObject

@property double speed;


@end


#import "SpaceCraft.h"
#import "BlackHole.h"
#import "Planet.m"

@interface SpaceCraft()

@property (nonatomic, strong) BlackHole *hole;

- (void) travelTo:(Planet *)planet withSpeed:(double) speed;

@end

@implementation SpaceCraft

@synthesize speed;

- (void) travelTo:(Planet *)planet withSpeed:(double) speed
{
    
}

- (void) orbitSpaceCraft:(Planet *) planet atAltitude:(double) miles
{
    double actualSpeed = [self speed];
    Planet *saturn = [[Planet alloc] init];
    if ([[self hole] tooClose:self]) {
        [self travelTo:saturn withSpeed:actualSpeed];
    }
}


@end

In the @interface we can put private elements. I created a property called "hole" and a method travelTo:withSpeed. But, I want you to pay attention to method orbitSpaceCraft:atAltitude. Let's analyze details of this method.

The code below create a variable actualSpeed and put the value of the @property speed. How?

    double actualSpeed = [self speed];

Because [self speed] calls the method get (remember that I created this method when I wrote @synthesize speed)

To create an instance, we normally use a syntax like this:

    Planet *saturn = [[Planet allocinit];

[Planet alloc] returns an object and then we call init method to initialize. Of course, there are differents ways to create an instance, but this is the most common when we create our class. 

What are we doing in the code below?

    if ([[self holetooClose:self]) {

Well, because we are using the reserved word IF we can imagine that everything returns a boolean type. It's true! But, let's talk about it.

[self hole] called the method get and it returns a BlackHole object, then I called a method tooClose from this object. The code below shows the definition of this method in the BlackHole class:

- (BOOL) tooClose:(NSObject *) object;


Finally, in the code below I called the method travelTo:withSpeed. This syntax is closely to Java or C# way. The difference is the use of dot notation:

  [self travelTo:saturn withSpeed:actualSpeed];

However, we can use dot notation to properties in Objective-C. See the implementation of travelTo:withSpeed:

- (void) orbitSpaceCraft:(Planet *) planet atAltitude:(double) miles
{
    double actualSpeed = self.speed;
    Planet *saturn = [[Planet alloc] init];
    if ([self.hole tooClose:self]) {
        self.speed = actualSpeed * 2;
        [self travelTo:saturn withSpeed:actualSpeed];
    }

}

We can put object.property. This is very useful to write code.

If you are careful, you noted that travelTo:withSpeed has different implementations. I doubled the speed to scape from BlackHole (because i want to continue to write this blog). The question is, how can I do this without dot notation?

In the next post we'll talk about constructors (init methods).

See you soon...


Nenhum comentário:

Postar um comentário