-1

i want to call the method [self methodName]; in class method but it will not call in same class.

i tried

+(void)myMehod 
{
   [self methoName];
}
Jay Bhalani
  • 4,142
  • 8
  • 37
  • 50
sohil
  • 818
  • 2
  • 15
  • 38
  • Maybe with using a Singleton, or creating an instance in the class method? But you should give more details on what you want to do. – Larme Jun 03 '15 at 11:28

2 Answers2

2

The instance method will only be called via the instance of the respective class. And since as you are calling a instance method inside class method using self, here self will call only class methods that's why your instance method will not be called.

To call instance method from class method you should have an instance of the class inside the class method. There is a workaround to do so-

+(id)myMethod
{
    [[[self alloc] init] methodName]; //Raises compiler warning. 
}

-(void) methodName 
{
     NSLog(@"I am called");
}

But the above workaround will result in a leak as the created instance was neither released and neither it was of autorelease type.

Sanjay Mohnani
  • 5,947
  • 30
  • 46
1

If you want to call an instance method you need an instance to call the method on.

Try this workaround but it does not follow concept of Object Orientation

+(void)myMehod {
    [[[self alloc] init] methodName];
}

-(void)methodName {
}
Utsav Parikh
  • 1,216
  • 7
  • 14