In the previous lesson, we took at look at classes in Objective-C, and dove into class properties. Of course, you can’t talk about classes without methods, as well, so this shall be the topic for today. If you’re reading this, then you’re either brave, or you missed my warning from my previous post. If you’re the latter, then I suggest you promptly click here and spare yourself. As for the rest of you, I commend you soldiers. Now buckle up, cause we’re going to jump straight to the syntax.

Objective-C Method Calls

So, you might at some point say to yourself, “Hey, I really like communicating with my objects. How would I go about this?” The answer is like this:

[self.navigationController pushViewController:self.detailViewController animated:YES];

There. I did it. I finally showed you the method-call syntax. Happy? This is a line taken near the bottom of MasterViewController.m, and basically its telling the navigation controller to push in the DetailViewController. (If you recall from lesson 2, the navigation controller steps in and out of the hierarchy of view states via pushing and popping view controllers.) Now, there’s a few things to talk about in this line, so let’s go over it piece by piece.

  1. When a class references itself, it uses self rather than the AS3 syntax of this. However, like in AS3, both use super to reference the superclass.
  2. Dot notation can be used to access properties defined by @property, hence the (dot)navigationController.
  3. Methods must be accessed using the square-bracket notation. The square bracket notation is structured such that the scope precedes its method, delimited by a space and then wrapped in square brackets, like this: [object method].
  4. If a method has a parameter, you would pass a value in by following the method name with a colon and the value, like this: [object method:param];
  5. If a method has multiple parameters, the method name be broken up such that it linguistically described each parameter. This part is a bit tricky. Basically, the method name extends past each previous parameter, allowing you to fill in the gaps with parameter values. It would look something like this: [object setPositionToX:100 andY:50 andZ:0]; In this example, the method name is actually “setPositionToX: andY: andZ:”, but in implementation, you fill in the placeholders so it “reads” like it behaves.
  6. (BONUS) Methods that return a value do so, thankfully, the same way it would in AS3, so all you’d need is something like: result = [object method];
  7. (MORE BONUS) You can nest method calls with additional square-bracket syntax: [object method:[object2 method2]];
  8. (EVEN MORE BONUS) In #2, I said properties defined by @property can be accessed with dot notation, however, since these properties are synthesized with setter/getters, you can also call the getter directly using square-bracket syntax. So self.navigationController could have alternatively been written as [self navigationController].

Oh and by the way, the YES in the line above is actually a boolean value. In Objective-C, booleans are written as YES and NO, as opposed to true and false. We’ll go over datatype and common object syntax in more depth later. That being said, if we were to rewrite the line above in AS3 syntax, it would look more like this:

this.navigationController.pushViewController(this.detailViewController, true);

Objective-C Method Structure

Ok, so we covered how to call a method in Objective-C. Now, how do we declare the methods? Fortunately, if you managed to understand the syntax of calling, this part shouldn’t be overwhelming. Let’s take a look at a method already declared in MasterViewController.m, shall we?

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

This one is on line 17 of MasterViewController.m. As you can see, the method name is again written to be read the way it behaves. When you call it, you’re literally telling the object to “initialize with the nib name x in bundle y.” There’s obviously more happening than just that, so let’s break it down.

  1. The first symbol denotes whether the method is static or instance. (+ for static, - for instance).
  2. Next is the return type encased in parenthesis. The id is a keyword that acts as a wildcard pointer for objects. It’s like the * in AS3, except not for simple datatypes. Returning id is a convention for init methods, as it makes the instantiation of objects more friendly. Also, like AS3, void indicates that there is nothing returned.
  3. The method name follows the return type. Not much to say here.
  4. If the method has a parameter, it is indicated by a colon, followed by the parameter type in parenthesis and the variable name. Type declaration for parameters (as well as the return type) follow the same syntax as variables (as described in the previous lesson). Therefore, if the type is an object, you’d follow it with an asterisk to denote that it’s actually the object’s pointer that you’re accepting (or returning).
  5. If the method has an additional parameter, you would supplement the method name with something to describe it, followed by another colon and the parameter declaration.
  6. Repeat step 5 until you’re out of parameters.

Now for the obligatory AS3 translation:

public function init(nibName:String, nibBundle:Bundle):*;

That sums up the method declaration. This structure is used in both the class’s @interface and @implementation, which you’ll see when we *gasp* write some code in the next lesson. By the way, you may be curious as to why none of the methods listed in MasterViewController.m are actually in the interface. This is because the methods are defined in the superclass’s interface. All of these methods are technically overridden, however Objective-C has no keyword to explicitly identify it as such. Oh yeah, and while I’m making confessions, all methods are also public. (There are tricks to help reduce the exposure of methods you don’t actually want exposed, so don’t worry too much about it.)

Optional Parameters

One final point of discussion in this lesson is how to define optional parameters. In AS3, this is normally achieved by listing your required parameters, followed by optional parameters with default value assignments. An example might look something like this:

public function reset(value1:int=0, value2:int=-1):void;

Objective-C, unfortunately, doesn’t have a direct means of doing the same. Instead, we accomplish this by defining multiple methods, one for each parameter that can be omitted, such as the example below:

- (void)reset;
- (void)resetWithValue1:(NSInteger)val1;
- (void)resetWithValue1:(NSInteger)val1 value2:(NSInteger)val2;

On the plus side, Objective-C’s syntax allows us to do this elegantly. Looking at this from the outside, the calls are intuitive:

[myObj reset];
[myObj resetWithValue1:100];
[myObj resetWithValue1:100 value2:200];

All you have to do is extend the syntax to include the optional parameters you wish to pass, and once again, it always reads like it behaves. To fill out the method execution, it would likely look like the example below, where each method calls its more explicit counterpart and pass in the additional parameter’s default value.

- (void)reset {
    [self resetWithValue1:0];
}
- (void)resetWithValue1:(NSInteger)val1 {
    [self resetWithValue1:val1 value2:-1];
}
- (void)resetWithValue1:(NSInteger)val1 value2:(NSInteger)val2 {
    // reset logic here
}

That’s it for today! In the next lesson we’ll *gasp* write some code and make a few modifications to our project.

 

One Response to Flex to Objective-C: 4-Methods

  1. Mario says:

    Really enjoying the series. I like the pace of it, little bits of information one at a time so it hasnt been that overwhelming. I have to admit though that I will still need some time to digest it and get used to its.

    Cheers!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Set your Twitter account name in your settings to use the TwitterBar Section.