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

}
10
  • What does the error say? Commented Apr 1, 2016 at 6:39
  • "No known class method for selector" Basically I am using methods inside a class method i.e method starting with "+" Commented Apr 1, 2016 at 6: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 (+)? Commented Apr 1, 2016 at 6:50
  • 1
    I doubt that your message to someMethodA: is working fine being it an instance method. Check This thread for further understanding. Commented Apr 1, 2016 at 6:56
  • 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. Commented Apr 1, 2016 at 7:02

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.