objective c - Why do class_respondsToSelector and respondsToSelector behave different when sent to Class? -
i have spent quite time trying figure out how class_respondstoselector , respondstoselector can give different results. consider following class:
@interface dummy : nsobject - (void)test; @end @implementation dummy - (void)test {} @end my scenario try determine if class responds class method. piece reproduces problem:
class class = [dummy class]; if (class_respondstoselector(class, @selector(test))) nslog(@"class_respondstoselector: yes"); else nslog(@"class_respondstoselector: no"); if ([class respondstoselector:@selector(test)]) nslog(@"respondstoselector: yes"); else nslog(@"respondstoselector: no"); if remove declaration , implementation of -test, output of above no , no expected. however, running reads above (including -test), output produced following:
class_respondstoselector: yes
respondstoselector: no
the documentation says nothing whether respondstoselector works instances only, indicates whether receiver implements..., hence unable determine whether correct behavior or not. missing something?
update
graham lee provided link great discussion on problem.
the question asked class_respondstoselector() "do instances of class respond selector?"
the question asked -[nsobject respondstoselector:] "does particular instance (which "reciever") respond selector?"
you're sending respondstoselector: class object, instance of metaclass, , asking particular object.
to see same results class_respondstoselector(), either use +[nsobject instancesrespondtoselector:] or instance of class.
Comments
Post a Comment