Objective-C: Empty segment Selector

Source: Internet
Author: User

Objective-C: Empty segment Selector



Prelude

Objective-C has a personality. A distinctive feature is that method functions are segmented, that is, function names are not written together, but are split into N segments, which correspond to N parameters respectively, this greatly increases the readability of the Code.

#define WQ_FUNC_LOG NSLog(@"%s",__FUNCTION__)- (void)firstSegment:(id)firstObj secSegment:(id)secObj thirdSegment:(id)thirdObj{    WQ_FUNC_LOG;}

For convenience, I printed my method name in this method. After this signal is sent (generally this method is called)

 - (void)viewDidLoad{    [super viewDidLoad];              [self firstSegment:@"1" secSegment:@"2" thirdSegment:@"3"];}

We will get the following log output. We can clearly see that the selector name is firstsegment: secsegment: thirdsegment:

2013-07-05 16:34:13.977 Test[1828:c07] -[WQTestViewController firstSegment:secSegment:thirdSegment:]
Topic

OK. The prelude is complete. Enter the topic. If I delete the second parameter secobj in the function prototype, what will happen?

- (void)firstSegment:(id)firstObj secSegment:(id) thirdSegment:(id)thirdObj{    WQ_FUNC_LOG;}

If you use the xcode auto-completion method, you will find that

 - (void)viewDidLoad{    [super viewDidLoad];              [self firstSegment:@"1" secSegment:@"2" :@"3"];}

Run the following log:

2013-07-05 17:26:10.725 Test[1976:c07] -[WQTestViewController firstSegment:secSegment::]

The conclusion is that the method is declared as firstsegment: secsegment:. What about the parameter? Of course, it is not difficult to guess and verify.

- (void)firstSegment:(id)firstObj secSegment:(id) thirdSegment:(id)thirdObj{    WQ_FUNC_LOG;    NSLog(@"args[] = {%@ ,%@, %@};",firstObj,thirdSegment,thirdObj);}

When thirdsegment and thirdobj are both considered as parameters, we have to have such a bold conjecture: Can selector support empty segments? Does selector have no name?
Try to create a method without a name. The correct statement is that an empty Selector

- (void) :(id)obj{}- (void)viewDidLoad{    [super viewDidLoad];        SEL sel = @selector(:);    if ([self respondsToSelector:sel]) {        NSLog(@"ur right");    }}

Compile and run the program. The log output tells us that our conjecture is correct. Objective-C supports the null selector.

Conclusion

Objective-C supports empty segments selector, which obviously does not have good code readability. We usually do not use it like this. Why do we need to mention this? I just want to remind you that sometimes we may encounter a low-level bug that requires debugging for a long time due to carelessness or carelessness. In particular, the delegate method is named smelly and long ......

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.