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:
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.
This, of course, refer to the name of the method.
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.
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.
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. 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.