Objective-C Tutorial 1 – How to declare Objective-C Methods

New developers to Objective-C frequently have a hard time understanding the method declarations.

Tutorial Objective: To learn how to declare, implement and call Objective-C methods

Before we start, it’s best to understand the types of methods that can exist:

Step 1 – Understand the types of methods

1) + Class Methods - A class method is like a static method in Java or C#. It is not tied to an instance of a class and has no knowledge of the instance variables. Therefore you do not need to create an instance of a class to access its method. Class methods should be mainly used for: creating a new object, utility or shared instance for a singleton class.

Example:

Method Declaration

+ (id) createCarName:(NSString *)name withColour:(NSString*)colour

Method Execution

 [CarClass createCarName:@"Honda" withColour:@"Red"];

2) - Instance Methods - Instance methods are different from Class Methods because of their scope and relationship to their class instance. As a result, to execute an instance method, you must first declare an instance of the class and then use the newly declared class to call the instance method.

Example:

Method Declaration

- (id) createCarName:(NSString *)name withColour:(NSString*)colour

Method Execution

 CarClass *car = [[CarClass alloc] init];

[car createCarName:@"Honda" withColour:@"Red];

Did you notice the key differences between how the different types of methods are executed? The Class Method just takes the class name and uses it to call the method. The Instance Method first requires an object to be instantiated and use the newly created object to call the method.

Step 2: Declaring method definitions in your header file(*.h).

Here is an example of Class declarations that include both an instance method and class method declaration.

CarClass.h

@interface CarClass : NSObject 
- (void)createCarName:(NSString*)name withColour(NSString*)colour;
+ (void)createCarName:(NSString*)name withColour(NSString*)colour;
@end

CarClass.m

@implementation CarClass 
- (void)createCarName:(NSString*)name withColour(NSString*)colour{     
     NSLog(@"Hello");
 }
+ (void)createCarName:(NSString*)name withColour(NSString*)colour {
     NSLog(@"Hello");
}
@end

Step 3: Declaring methods with multiple arguments 

1. Methods with no parameter

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

+ (void) createCarName;

- (void) createCarName;

2. Methods with a single parameter

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

+ (void) createCarName: (NSString *) name;

- (void) createCarName: (NSString *) name;

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) createCarName: (NSString *) name withColour : (NSString *) colour;

- (void) createCarName: (NSString *) name withColour : (NSString *) colour;

 Step 4: Calling the methods

1. Create a file called IOSTutorial.h and declare the following information:

#import <Foundation/Foundation.h>
#import "CarClass.h"

int main (int argc, const char * argv[])
{
    @autoreleasepool {
         CarClass *car = [[CarClass alloc] init]; 
         [car createCarName:@"Honda" withColour:@"Red];
         [CarClass createCarName:@"Honda" withColour:@"Red"];
     }
    return 0;
}

Running this in XCode should show the results in the console log.

Step 5: Do it yourself!

1. Start up Xcode

2. Create files IOSTutorial.h, CarClass.h, CarClass.m

3. Declare the methods, header and implementation files like above.

4. Go and Run the Project in XCode and view the results!

5. Voila! You can now declare the two types of methods(instance and Class). Implement them in their corresponding implementation file and execute them from an external file, in this case IOSTutorial.h.

Do you have any questions about this Tutorial? If so, please leave them in the comment section!

Share Button

Leave a Reply