I'm a .NET programmer new to objective-c, and I'm struggling to understand some nuts and bolts syntax. For example, how should I parse this method signature:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
I understand what the "-" char means, and (UITableViewCell *) defines the return type. But the rest has me confused.
(1) (2) (3) (4) (5) (6) (7) (8)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- "-" Defines an instance method
- Returns
UITableViewCell
pointer - First part of the method signature named "tableView"
- Takes a
UITableView
pointer - With the local variable name "tableView"
- Second part of the method signature "cellForRowAtIndexPath"
- Takes a
NSIndexPath
pointer - With the local variable name "indexPath".
The actual method signature is: tableView:cellForRowAtIndexPath:
.
Read Apple's documentation, like Objective-C: A Primer. It's explained right there. You know, the maker (Apple or Microsoft) has a lot of documentation on their site ...
Objective-C uses named, inline parameters for methods. (As bblum points out in the comment below, this style of parameters are sometimes called "interleaved".) This is a reflection of it's heratage as a mix of C and SmallTalk syntax. The trailing colons denote the names of the parameters to the method. For your method, the full name of the method is referred to as tableView:cellForRowAtIndexPath:
. It takes two parameters, a pointer to a UITableView
, and pointer to a NSIndexPath
. In a java-like language, this method signature would look something like:
public UITableViewCell cellInTableViewForRowAtIndexPath(UITableView tableView, NSIndexPath indexPath);
Every foo:(bar)baz
defines a parameter, for example
- (id)initWithTitle:(NSString *)title
message:(NSString *)message
delegate:(id)delegate
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSString *)otherButtonTitles, ... {
defines a method with five* parameters.
The stuff before the :
is part of the name of the method. In this example, the method's name is
initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:
The stuff between the (…)
is the type of that argument. Here, we see that the first argument must be an NSString*
.
Finally it's the name of the parameter.
(*: Sometimes there is sometimes a , ...
, like in here, indicating it's a variadic method.)
The method is called in the syntax
id result = [theAllocedAlertView initWithTitle:@"title"
message:@"message"
delegate:someDelegate
cancelButtonTitle:@"cancel button title"
otherButtonTitles:@"other", @"button", @"titles", nil];
So the name of the method is repeated (in order!), and the parameter names are substituted by the actual arguments.
In C#, the corresponding function signature would look like
object InitWithTitleAndMessageAndDelegateAndCancelButtonTitleAndOtherButtonTitles(
string title,
string message,
object delegate,
string cancelButtonTitle,
params string[] otherButtonTitles);
and called like
object result = theAllocedAlertView.InitWithBlahBlahBlahAndOtherButtonTitles(
"title",
"message",
someDelegate,
"cancel button title",
"other", "button", "titles");
The method selector is:
tableView:cellForRowAtIndexPath:
where each value after the colon is a parameter. The signature is meant to read like an English sentence, i.e. "The TableView's cell for a row at this index".
If this were written in another language it might look like this
// @param (UITableView *) tableView
// @param (NSIndexPath*)indexPath
// @return UITableViewCell
- (UITableViewCell *) someFunctionName(tableView, indexPath) {
}
Thats roughly speaking of course. It would not be written like this in objective-c. However I believe it is possible to write a good chunk of your program in c++
精彩评论