Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 783
jini Avatar asked Apr 23 '11 03:04

jini


3 Answers

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.

like image 186
cobbal Avatar answered Nov 15 '22 13:11

cobbal


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.

like image 3
Caleb Avatar answered Nov 15 '22 11:11

Caleb


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

like image 1
Krishnabhadra Avatar answered Nov 15 '22 13:11

Krishnabhadra