Can someone explain function names in Objective-C?

Possible duplicate:
Method Syntax in Objective-C

So, I completely get more general functions, such as:

-(void)viewDidUnload{ self.controllers = nil; [super viewDidUnload]; } 

However, based on a different programming background, I sometimes struggle with something like:

 -(NSInteger) tableView: (UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [self.controllers count]; } 

So, I know that the function returns NSInteger. However, I'm a little confused about how to mentally organize the rest of the ETC name function. I need to be able to visualize some structure. As in this case, is the name of the function numberOfRowsInSection with a parameter called a section?

Help in this matter will be appreciated.

+7
source share
5 answers

You can think about it, like other programming languages, by looking at

 [object action:var withFoo:baz] 

but

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

All before the colons are part of the method name, and everything after the arguments, so the method name alternates with the arguments passed to the method.

+6
source

Yes, the way Objective-C mixes arguments with parts of a method name seems strange at first. In this regard, everyone goes through a short adjustment period. But give it time - after a while you may not want to ever see a list without name parameters with comma-delimited ones.

In C ++, you would say something like:

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

You know what these meanings mean, right? There are four, therefore, obviously, the parameters are in red, green, blue, alpha order. Or was it alpha, red, green, blue? Of course, it can also be hue, saturation, value, alpha ... well, that doesn't really matter, because you can always just watch it.

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 definitely still need to consult the documentation from time to time to remind yourself what exactly this method does, or what methods the class provides. But you will not often go to the documents to find out which parameter goes there.

+3
source

Target C has a descriptive way of writing methods.

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

This is a tableView method that returns an NSInteger that takes two arguments - a UITableView link and an integer section. Now consider numberofRowsInSection as a description of what is an argument. Look in this example

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

and I can call these 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 the first one is easy to read, if you indicate that each variable ... I hope this helps

Also see that I used the word method instead of functions that are usually c objects.

+1
source

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

This is a method that takes 2 arguments, UITableView and NSInteger. It is indicated as follows: -tableView:numberOfRowsInSection:

0
source

Yes, basically that. In obj-C, full method names include arg names. I believe the convention came from Smalltalk.

0
source

All Articles