开发者

What's the difference between these Objective C method styles?

开发者 https://www.devze.com 2023-02-06 20:37 出处:网络
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 UIWebViewDelegat

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'开发者_如何学Cs 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.


You can have multiple web views or table views on the screen at the same time. During a callback of the methods you listed, you can take a look at the web views or table views' tag property to decide which web view or table view is responsible for such a call back.


It's part of Apple's own Objective-C coding guidelines regarding delegate methods: http://akos.ma/gt

Delegate methods (or delegation methods) are those that an object invokes in its delegate (if the delegate implements them) when certain events occur. They have a distinctive form, which apply equally to methods invoked in an object’s data source:

  • Start the name by identifying the class of the object that’s sending the message:
- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(int)row;
- (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename;

So, to answer your question, all of the methods you quote follow the same pattern: the first (and sometimes the only) parameter of the method is the object calling the delegate method.


Why isn't the second one simply written as webViewDidFailLoadWithError...

Because then it would have to be:

- (void) webViewDidFailLoadWithError:(NSError *)error {

}

which lacks the first argument, so when the method is called on the delegate, you wouldn't know which webview returned the error.

...or why doesn't the first one match the second style?

because then it would have to be

- (void)webView:(UIWebView *)webView DidFinishLoad {
}

which is not a valid method name, since the 'DidFinishLoad' part is not preceded by an argument --that is, you can't just add parts to a method name without matching them with an argument (with the only exception that of a method that receives no arguments at all).

I hope that makes it clear.

0

精彩评论

暂无评论...
验证码 换一张
取 消