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 ......