Objective-C method overload and objective-c method overload
The Objective-C method overload-Selector identifier determines the role
Beautiful Life of the sun and fire god (http://blog.csdn.net/opengl_es)
This article follows the "signature-non-commercial use-consistency" creation public agreement
Reprinted please keep this sentence: Sun huoshen's beautiful life-this blog focuses on Agile development and mobile and IOT device research: iOS, Android, Html5, Arduino, pcDuino, otherwise, this blog post is rejected or reprinted. Thank you for your cooperation.
Overload 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;
Add this sentence to the implementation of each of the above methods to print the signature of the current method:
NSLog(@"%@", NSStringFromSelector(_cmd));
Call the preceding 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 result is as follows:
2015-02-28 12:14:02.724 TestWebView[1490:371780] test2015-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 of XCode indicates that the data type cannot be used to distinguish between overloaded methods:
According to the error message of XCode, the parameter names before the colon are used to distinguish the method signature. The parameter names and data types are the same and do not work, this is due to the fact that the components of selector are preemptible:
This can be summarized as follows:
1. The number of parameters determines the method signature;
2. method with the same number of parameters. parameter names are used to distinguish between overload
3. Data Types and parameter names cannot be used to differentiate methods.
4. The component of selector determines that the method signature consists of the part before the colon is used to differentiate methods.