Objective-c method Overloading-Selector identification plays a decisive role

Source: Internet
Author: User



Objective-c method Overloading-Selector identification plays a decisive role



The beautiful Life of the Sun Vulcan (http://blog.csdn.net/opengl_es)



This article follows "Attribution-non-commercial use-consistent" authoring public agreement



Reprint Please keep this sentence: Sun Vulcan's Beautiful Life-this blog focuses on Agile development and mobile and IoT device research: IOS, Android, HTML5, Arduino, Pcduino , Otherwise, the article from this blog refused to reprint or re-reproduced, thank you for your cooperation.






Overloaded method declaration:


- (void)test;
- (void)test:(NSString *)command;
- (NSString *)test:(NSString *)command param:(NSString *)param;
- (NSString *)test:(NSString *)command param:(NSString *)param result:(NSString *)result;
- (NSString *)test:(NSString *)command result:(NSString *)result param:(NSString *)param;
- (NSString *)test:(NSString *)command :(NSString *)result :(NSString *)param;
- (NSString *)test:(NSString *)command param:(NSString *)param callback:(NSString *)callback;



In the implementation of each of these methods, this sentence is added to print the signature of the current method:





NSLog (@ "%@", Nsstringfromselector (_cmd));






Call the above methods in the following order:


    [interface test];
    [interface test:@"command"];
    [interface test:@"command" param:@"param"];
    [interface test:@"command" param:@"param" result:@"result"];
    [interface test:@"command" result:@"result" param:@"param"];
    [interface test:@"command" :@"result" :@"param"];
    [interface test:@"command" param:@"param" callback:@"callback"];






The output results are as follows:





2015-02-28 12:14:02.724 TestWebView[1490:371780] test
2015-02-28 12:14:02.724 TestWebView[1490:371780] test:
2015-02-28 12:14:02.724 TestWebView[1490:371780] test:param:
2015-02-28 12:14:02.724 TestWebView[1490:371780] test:param:result:
2015-02-28 12:14:02.725 TestWebView[1490:371780] test:result:param:
2015-02-28 12:14:02.725 TestWebView[1490:371780] test:::
2015-02-28 12:14:02.725 TestWebView[1490:371780] test:param:callback:





The error message from XCode indicates that the data type cannot be used to differentiate overloaded methods:









The error message from XCode shows that the parameter name before the colon is the basis for distinguishing the signature of the method, and that the parameter name and data type do not work, because the constituent elements of selector are the pre-run parts:










This can be summed up:



1, the number of parameters determines the signature of the method;



2, the same parameter number method, the parameter name is used to distinguish the overloaded



3. Data types and formal parameter names cannot be used to differentiate methods



4. The constituent elements of selector determine that the method signature of the part before the colon is used to differentiate the method









Objective-c method Overloading-Selector identification plays a decisive role


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.