Home > DeveloperSection > Interviews > How do I make a static and a class method in Objective C?

Posted on    July-16-2015 12:47 AM

 iPhone iPhone  Ios  Objective C 
Ratings:
 1 Answer(s)
  378  View(s)
Rate this:

Tarun Kumar

Total Post:107

Points:535
Posted on    July-16-2015 12:47 AM

A class method is a method whose execution is scoped to the method’s class. It does not require an instance of an object to be the receiver of a message.

An instance method is a method whose execution is scoped to a particular instance of the class. In other words, before you call an instance method, you must first create an instance of the class. Instance methods are the most common type of method.

Complete Example:

#import< Foundation/Foundation.h>

 

@interface SampleClass:NSObject

    + (void)classMethod;

    - (void)instanceMethod;

@end

 

@implementation SampleClass

    +(void)classMethod{

        NSLog(@"This is a class method.");

    }

    -(void)instanceMethod{

        NSLog(@"This is a instance method.");

    }   

@end

 

int main()

{

    [SampleClass classMethod];   // calling class method

 

    SampleClass *object = [[SampleClass alloc]init];

    [object instanceMethod];      // calling instance method

 

    return 0;

}


Modified On Jul-16-2015 02:14:49 AM

Don't want to miss updates? Please click the below button!

Follow MindStick