开发者

Can someone explain function names in Objective-C? [duplicate]

开发者 https://www.devze.com 2023-02-28 00:23 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Method Syntax in Objective C
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Method Syntax in Objective C

So I totally get the more common functions like:

-(void)viewDidUnload{

    self.controllers = nil;
    [super viewDidUnload];
}

However coming from a different programming background, I sometimes struggle with something like:

-(NSInteger) tableView: (UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{

    return [self.controllers count];

}

So I know the function returns a NSInteger. However I do get a little confused in how to mentally organize the rest of the function n开发者_开发百科ame ETC. I need to be able to visualize some structure. Like in this case is the function name numberOfRowsInSection with the parameter called section?

Help in this matter would be appreciated.


You can think of it like other programming languages by looking at

[object action:var withFoo:baz]

as

object."action:withFoo:"(var, baz)

Everything before colons is part of the method name, and everything after is arguments, so the name of the method is interleaved with the arguments passed to the method.


Yes, the way that Objective-C mixes arguments with parts of the method name seems weird at first. Everyone goes through a short adjustment period in this respect. But give it time -- after a little while, you may not want to ever see a comma-separated, parenthesized list of nameless parameters ever again.

In C++, you'd say something like:

Color *color = new Color(0.5, 0.7, 0.2, 0.8);

You know what those values mean, right? There's four of them, so obviously the parameters are in red, green, blue, alpha order. Or was it alpha, red, green, blue? Of course, it could also be hue, saturation, value, alpha... well, it doesn't really matter because you can always just look it up.

In Objective-C, you say:

UIColor *color = [[UIColor alloc] initWithRed:0.5 green:0.7 blue:0.2 alpha:0.8];

Isn't that better? You'll definitely still need to consult the documentation from time to time to remind yourself exactly what a method does, or what methods a class makes available. But you won't often find yourself consulting the docs just to figure out which parameter goes where.


Objective C has a descriptive way of writing methods..

-(NSInteger) tableView: (UITableView *)tableView numberOfRowsInSection:(NSInteger)section{}

It is a methods tableView, returning an NSInteger , takes two argument - a UITableView reference and an integer section. Now consider numberofRowsInSection as a description of what the argument is all about.Look at this example

-(NSInteger) calculateSum:(NSInteger)operand1 secondOperand:(NSInteger)operand2 andThirdOperand:(NSInteger)operand3{}

and I can call this methods as [self calculateSum:var1 secondOperand:var2 andThirdOperand:var3];

Here "secondOperand" and "andThirdOperand" are not essential, I can write the above method as

-(NSInteger) calculateSum:(NSInteger)operand1 :(NSInteger)operand2 :(NSInteger)operand3{}

and call this method as

[self calculateSum:var1 :var2 :var3];

But first one is easy to read, if you tell what each variable is ..Hope this helps

Also see I used the word method instead of functions which is normally the objective c way..


(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

This is a method that accepts 2 arguments, a UITableView and an NSInteger. It's notated like this: -tableView:numberOfRowsInSection:.


Yep that's basically it. In obj-C the full method names include the arg names. I believe that convention originated from Smalltalk.

0

精彩评论

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