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