0

I am accessing instance method in a class method by using self in singleton class it shows error, but if I try to access instance method with parameter using self then its perfectly working, so basically i want to know the reason, why i am not able to access this.

like

-(void)someMethodA:(NSString *)paramA withParamB:(NSString *)paramB;

-(void)someMethodB;

if i try to use "someMethodA" with self then it works perfectly fine, but accessing "someMethodB" using self it gives error

+(void)someMethod

{

   [self someMethodA:@"string" withParamB:@"string"]; works fine

   [self someMethodB]; gives error

}
  • What does the error say? – Gandalf Apr 01 '16 at 06:39
  • "No known class method for selector" Basically I am using methods inside a class method i.e method starting with "+" – Prashant Goyal Apr 01 '16 at 06:43
  • You can't use `self` within a class method to refer to an instance method - `self` doesn't refer to an instance in this case. Are you sure that `someMethodA` isn't defined as a class method also (+)? – Paulw11 Apr 01 '16 at 06:50
  • 1
    I doubt that your message to `someMethodA:` is working fine being it an instance method. Check [This thread](http://stackoverflow.com/questions/2121880/call-instance-method-from-class-method) for further understanding. – Gandalf Apr 01 '16 at 06:56
  • Well, I just tested it and I cannot refer to an instance method via `self` inside a class method, regardless of whether it has parameters or not. Perhaps something in your code is causing the compiler not to flag the issue but I am sure you would get a runtime exception if you did run the code. – Paulw11 Apr 01 '16 at 06:58
  • thanks @Paulw11, so I cannot able to call instance method in class method, why so? – Prashant Goyal Apr 01 '16 at 07:00
  • 1
    As I said above and as the answers on the question that @Gandalf linked to explains, instance methods need to operate on an instance. A class is not an instance. You can call the instance method on your singleton instance, but not on `self` directly. – Paulw11 Apr 01 '16 at 07:02
  • thanks @Gandalf , now I cleared much about instance and class methods. – Prashant Goyal Apr 01 '16 at 07:06
  • @Paulw11 - Just a correction, `class methods` can be called on `self`. The thread i linked to has explanation from _bbum_ that `self` has different meaning in `class methods` and `instance methods`. That's why we can call `+alloc` on `self` from within a `+sharedObject` method while creating a singleton. – Gandalf Apr 01 '16 at 07:09
  • Yes, perhaps I wasn't clear when I said that "You can't use self within a class method to refer to an instance method". You can, of course, use `self` to invoke other class methods because in a class method context, `self` is the class. You can even use `self` in a class method to invoke superclass class methods as inheritance is respected – Paulw11 Apr 01 '16 at 07:12

1 Answers1

0

Instance methods(methods that start with '-' ) can only be accessed by other instance methods, while class methods(methods that start with '+') can be accessed from anywhere by using [CLASS_NAME methodName]

So if you need to access someMethodB from your class method, then simply update it to class method as below:

+(void)someMethodB;
gunjot singh
  • 2,578
  • 20
  • 28