Objective-C method overload and objective-c method overload

Source: Internet
Author: User

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.



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.