HOW DO I MAKE A STATIC AND A CLASS METHOD IN OBJECTIVE C?

Tarun Kumar

Total Post:107

Points:535
Posted by  Tarun Kumar
 645  View(s)
Ratings:
Rate this:
  1. Tarun Kumar

    Post:107

    Points:535
    How do I make a static and a class method in Objective C?

    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!