There seems to be two standard ways of writing methods in Objective C, and I can’t quite grasp what the difference is and why one is used rather than the other. For example, from the UIWebViewDelegate:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
}
- (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
}
Why isn’t the second one simply written as webViewDidFailLoadWithError, or why doesn’t the first one match the second style?
Another example, this time from UITableViewDataSource:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 0;
}
How come numberOfSectionsInTableView doesn’t follow the same format as the other methods?
I’m sorry if this is a very simple question – it’s just been bugging me for a while now and I’d like to get it clear in my head!
Thanks in advance for your help.
It all comes down to the number of arguments. Pretty much every delegate method passes the sender of the method as its first argument. If the method does not need further arguments, the method signature is in the first style, otherwise it is in the second, following the Cocoa convention to name each argument.
Unfortunately, it is not possible to append more text to a method signature after the last argument. If it were, I am sure Apple would rather name the method
- (void) webView:(UIWebView *)webView didFinishLoad.Edit: There was an interesting discussion recently here on Stack Overflow on the history of this syntax decision: Why must the last part of an Objective-C method name take an argument (when there is more than one part)? where even Brad Cox, creator of Objective-C, chimed in.