2

In my code I want to know if a method is a class method or an instance method. The code I am currently using works, but I wonder if there is a beter way.

Current code to "detect" if it is a class method or instance:

Method method = class_getInstanceMethod(class, selector);
if (method) {
  __strong Class object = [[class alloc] init];
  objc_msgSend(object, selector);
}else {
  method = class_getClassMethod(class, selector);
  if (method) {
    objc_msgSend(class, selector);
  }
}
3
  • I am pretty sure your code could not be improved. You can change objc_msgSend to performSelector: if you really want but there is no real sense to do so. Commented Feb 21, 2014 at 15:56
  • 2
    I'm very curious as to your motivation here. Every signature in objective=c tells you whether it's a class or instance method so I'm curious why you would need this at run time Commented Feb 21, 2014 at 15:56
  • @NuclearGhost I want to call a method on a object which might not be there. This could be an instance method or a class method. Warren Burton why invert since it will only instantiate if it is a class method. This object file might not be imported. And this is a generic function to do so. I could make two functions, one for class other for instance. Commented Feb 21, 2014 at 15:58

1 Answer 1

3

There's very little that you can improve beyond two if statements. You can use respondsToSelector: method instead, but since you do not have an object to start with, you will end up with an if inside an if rather than an a better-looking else if:

if ([class respondsToSelector:selector]) {
    // Call class method
} else {
    id object = [[class alloc] init];
    if ([object respondsToSelector:selector]) {
        // Call instance method
    }
}

If you could modify your program flow to start with an object instead of a class, you coud do this instead:

if ([object respondsToSelector:selector]) {
    // Call instance method
} else if ([[object class] respondsToSelector:selector]) {
    // Call class method
}
Sign up to request clarification or add additional context in comments.

6 Comments

Method doesn't have a respondsToSelector
@MouNtant a method is a selector, an object has a selector. This is far simpler code than yours posted in the question which is overkill
@MouNtant I did not realize that you've got no object in the beginning. I edited the answer to start with a class method the same way that you did.
@SimonMcLoughlin I feel like this code is basically the same and not much simpler. (Code in answer has changed)
@MouNtant I made the same mistake, didn't notice the object init. Still think its cleaner and more readable in this manner. Also why do you keep switching between and upvote / downvote. Either way its still solid code
|

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.