Declaring Objective-C Methods


A language element that took a third if not a second look before I understood it is declaring an Objective-C method. Since I am a newbie as far as coding in Objective-C is concerned, I just accepted things for what is, understanding a bit first, making something work, and learn more about it as I progressed. When you declare an Objective-C method, it follows this basic format:

1. Methods with no parameter

<method type> (<return type>) <method name>;


+ (void) doLogin;

- (void) doLogin;

2. Methods with a single parameter

<method type> (<return type>) <method name>: (<argument type>) <argument name>;


+(void) doLoginWithUserId: (NSString *) userId;

- (void) doLoginWithUserId: (NSString *) userId;

3. Methods with 2 parameters

<method type> (<return type>) <method name>: (<argument type>) <argument name> <argument 2 label>: (<argument 2 type>) <argument 2 name>;


+(void) doLoginWithUserId: (NSString *) userId andPassword : (NSString *) pwd;

- (void) doLoginWithUserId: (NSString *) userId andPassword : (NSString *) pwd;

The following are the elements as mentioned in the syntax:

Method Type

Replace <method type> with either a + or a –. The + method type means that the method is a class method or in C#/Java world means that it is a static method. It is a method which can be invoked without instantiating the class. The – method type, on the other hand, is an instance method or a method which can be invoked only when the class has already been instantiated.

Using the sample code in item 1 format above and given each method is declared in separate classes named LoginClass, the class methods or + methods can be invoked as follows:


[LoginClass doLogin];

[LoginClass doLoginWithUserId:@”jojit”];

[LoginClass doLoginWithUserId:@”jojit” andPassword:@”password”];

The instance method or the – method type can be invoked by declaring a variable of type LoginClass and instantiating it as follows.


LoginClass *loginObj = [[LoginClass alloc] init];

[loginObj doLogin];

[loginObj doLoginWithUserId:@”jojit”];

[loginObj doLoginWithUserId:@”jojit” andPassword:@”password”];

Return Type, Argument Type, and Argument 2 Type

Replace <return type>, <argument type>, and <argument 2 type> with valid data types like void, int, NSString, etc. Note the asterisk after the NSString arguments in above examples, this is used when you are using an object data type rather than primitive data types.

Method Name

This, of course, refer to the name of the method. Smile

Argument Name and Argument 2 Name

These elements are the same as method arguments in other languages like C#, VB and Java, i.e. it is used for passing parameters to the methods. I would admit that I haven’t researched if there is such thing as in/out or ByVal/ByRef parameters in Objective-C. Smile

Argument 2 Label

Why is there an Argument 2 Label but no Argument 1 Label? I don’t know but actually the label is optional. You may or may not use a label for the arguments 2 onwards but along the way, I realized how to better name my methods and arguments. Winking smile

I accepted these things as they were initially but along the way I kept on asking “does it mean all methods are public?”. This is because we just want some methods to be invoked internally within the class. I tried using C functions believing that they are always private but I have to verify this some more. Smile C functions have a different declaration syntax compared to Objective-C methods. I won’t discuss it here but this is interesting to note. Hopefully, I can discuss it in another post or at least post a link to another blog discussing it.

After about a week in coding and several lines of code, I am still itching at finding how to make my methods private. I found the answer just today and it prompted me to post this blog again after about a month hiatus to at least lessen my blogging backlog. I will discuss about that in my next post.

This work is licensed under a Creative Commons Attribution By license.
About these ads
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

3 Responses to Declaring Objective-C Methods

  1. bbum says:

    Objective-C’s method declaration syntax pattern is called “interleaved declaration” and is borrowed from SmallTalk.

    Note that Objective-C does not have “named” or “keyword” arguments (not that you said it did — just that some people say that and implies something that isn’t true; reordering or optionality).

    Note also that the method name — the selector — is all parts of the declaration besides the arguments and types. That is, you declared a method named doLoginWithUserId:andPassword: in the above.

    Argument labels isn’t a common reference, either. The bits to the left of the colon are “parts of the method name”. Generally, we really don’t break them out for discussion.

    Note that the parts are optional. That is, you could have declared that method as doLoginWithUserId::. That is a different method name — a different selector. It is also ugly and leads to horrible to read/maintain code. Don’t do that! :)

    Objective-C doesn’t have private methods at runtime. At compile time, a method is effectively private if you keep the declaration out of the header.

    If you want to know more about how, exactly, Objective-C does method dispatches, start here: http://www.friday.com/bbum/2009/12/18/objc_msgsend-part-1-the-road-map/

    If you want to know more about why Objective-C does not have private methods, this StackOverflow post might be helpful: http://stackoverflow.com/questions/2158660/why-doesnt-objective-c-support-private-methods

    You might also find Class Extensions to be useful: http://www.friday.com/bbum/2009/09/11/class-extensions-explained/

    • Jojit Soriano says:

      @bbum: Thank you very much for dropping by my blog to share your expertise. I am beginning to learn Objective-C and I can’t help but look for something in Objective-C that I know is in .Net. This, somehow, helps me fasttrack learning. Aside from terminologies like selectors, some rules are also different like my concern about private methods. It is very welcoming to receive help from Objective-C veterans like you through comments, blogs, and responses in forums.

      About the argument labels, I agree that without these the method signature will look ugly. This way of method naming somehow encourages devs to think creatively to name the method that will suit this convention. I initially thought of naming doLoginWithUserId:andPassword differently but when I tried to call it, the signature became doLogin:Password. It’s funny that I have to rename my methods to make it something more like a conversational english. :)

      About the private methods, I have checked the links and will digest them some more. I haven’t came across with them before. What I found so far, which was the one I planned to post next, was about Categories and taking the methods off the header or interface declaration.

      StackOverflow.com is indeed a very helpful site even when I’m developing .Net applications. Since I’m new to the iPhone/Mac world, I’m still building on a very good list of reference blogs and sites.

      Feel free to drop by again.

      Thanks.

  2. dubinine says:

    Thank you for this sort of recap. I was able to get involved quickly.
    About the private method, my understanding was I could put it outside of .h and it became private in this way.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s