How to determine whether a method is defined in a class in objective-C?

Source: Internet
Author: User
In C #, the metadata can be analyzed through reflection. Example: Code As follows:
Using system; using system. reflection; namespace Hello {class program {static void main (string [] ARGs) {If (ismethoddefined (typeof (utils), "helloworld") {console. writeline ("methods in the utils class helloworld");} else {console. writeline ("there is no way to helloworld in utils class");} console. readkey ();} /// <summary> /// determines whether a class has a "specified name" method. /// </Summary> /// <Param name = "type"> </ param> /// <Param name = "methodname"> </param> /// <Returns> </returns> static bool ismethoddefined (type, string methodname) {bool result = false; foreach (memberinfo m in type. getmembers () {If (M. name = methodname) {result = true; break ;}} return result ;}} public static class utils {public static void helloworld () {console. writeline ("Hello world! ");}}}

In obj-C, it is determined by selector.

SAMPE. h

 
# Import <Foundation/Foundation. h> @ interface sample: nsobject {}-(void) print :( nsstring *) MSG; @ end

Sample. m

 
# Import "sample. H" @ implementation sample-(void) print :( nsstring *) MSG {nslog (@ "% @", MSG);} @ end

Main function:

 
# Import <Foundation/Foundation. h> # import "sample. H "int main (INT argc, const char * argv []) {ngutoreleasepool * Pool = [[ngutoreleasepool alloc] init]; sample * s = [sample new]; if ([s respondstoselector: @ selector (print :)]) // This row is used to determine whether the print {[S print: @ "Hello World"];} else {nslog (@ "% @", @ "print" is not defined in the sample class);} [S release]; [pool drain]; return 0 ;}
Related Article

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.