Use of respondsToSelector

Source: Internet
Author: User

-(BOOL) isKindOfClass: classObj is used to determine whether it is an instance of a class or its subclass.
-(BOOL) isMemberOfClass: classObj is used to determine whether it is an instance of a class.
-(BOOL) respondsToSelector: selector is used to determine whether there is a method named after a certain name (encapsulated in a selector object for passing)
+ (BOOL) instancesRespondToSelector: selector is used to determine whether an instance has a method named after a certain name. the difference is that the previous method can be used on instances and classes, and this method can only be used on classes.
-(Id) specify mselector: selector
 

SEL sel = @ selector (start :); // specify action

If ([obj respondsToSelector: sel])

{// Determine whether the object has a corresponding method

[Obj into mselector: sel withObject: self]; // call the selector Method

}

Use [[UIApplication sharedApplication] keyWindow] to find the main window object of the application

 

 

RespondsToSelector determines whether a method is implemented

Tester. h

 

# Import <Foundation/Foundation. h> @ interface Tester: NSObject {}-(void) test :( NSString *) msg;-(void) noibd; @ end

Tester. m

# Import "Tester. h "@ implementation Tester-(void) test :( NSString *) msg {NSLog (@" % @ ", msg) ;}@ end note: noibd method is not implemented
Main. m

 

# Import <Foundation/Foundation. h> # import "Tester. h "int main (int argc, const char * argv []) {ngutoreleasepool * pool = [[ngutoreleasepool alloc] init]; id tester = [Tester alloc] init]; // Note: Here id SEL testSelector = @ selector (test :); SEL notImpSelector = @ selector (noibd :); if ([tester respondsToSelector: testSelector]) {// tester. m implements the test method [tester test: @ "invoke test method"];} if ([tester respondsToSelector: notImpSelector]) {// test. m does not implement this master, go to [tester noibd];} [pool drain]; return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.