Possible Duplicate:
Method Syntax in Objective C可能重复:目标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?
所以我知道该函数返回一个NSInteger。但是我对如何在心理上组织其余功能名称ETC感到困惑。我需要能够想象一些结构。就像在这种情况下的函数名称numberOfRowsInSection一样,参数名为section?
Help in this matter would be appreciated.
在此问题的帮助将不胜感激。
5 个解决方案
#1
6
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.
冒号之前的所有内容都是方法名称的一部分,后面的所有内容都是参数,因此方法的名称与传递给方法的参数交错。
#2
3
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.
是的,Objective-C首先将参数与方法名称的部分混合的方式似乎很奇怪。在这方面,每个人都经历了一个短暂的调整期。但是给它时间 - 过一会儿,你可能不希望再次看到逗号分隔的,带括号的无名参数列表。
In C++, you'd say something like:
在C ++中,你会说:
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.
你知道这些价值观是什么意思吗?其中有四个,显然参数是红色,绿色,蓝色,alpha顺序。还是阿尔法,红色,绿色,蓝色?当然,它也可能是色调,饱和度,价值,阿尔法......好吧,它并不重要,因为你总能看到它。
In Objective-C, you say:
在Objective-C中,你说:
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.
那不是更好吗?你肯定还需要不时查阅文档,以提醒自己确切的方法是什么,或者一个类提供什么方法。但是你不会经常发现自己正在查阅文档只是为了弄清楚哪个参数在哪里。
#3
1
Objective C has a descriptive way of writing methods..
Objective C有一种描述性的编写方法。
-(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
它是一个方法tableView,返回一个NSInteger,接受两个参数 - 一个UITableView引用和一个整数部分。现在考虑将numberofRowsInSection描述为参数的全部内容。看看这个例子
-(NSInteger) calculateSum:(NSInteger)operand1 secondOperand:(NSInteger)operand2 andThirdOperand:(NSInteger)operand3{}
and I can call this methods as [self calculateSum:var1 secondOperand:var2 andThirdOperand:var3];
我可以将此方法称为[self calculateSum:var1 secondOperand:var2 andThirdOperand:var3];
Here "secondOperand" and "andThirdOperand" are not essential, I can write the above method as
这里“secondOperand”和“andThirdOperand”不是必不可少的,我可以把上面的方法写成
-(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..
另请参阅我使用单词方法而不是通常是客观方式的函数。
#4
0
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
(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:
.
这是一个接受2个参数的方法,UITableView和NSInteger。它的注释如下:-tableView:numberOfRowsInSection:。
#5
0
Yep that's basically it. In obj-C the full method names include the arg names. I believe that convention originated from Smalltalk.
是的,基本上就是这样。在obj-C中,完整的方法名称包括arg名称。我认为该惯例源于Smalltalk。
#1
6
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.
冒号之前的所有内容都是方法名称的一部分,后面的所有内容都是参数,因此方法的名称与传递给方法的参数交错。
#2
3
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.
是的,Objective-C首先将参数与方法名称的部分混合的方式似乎很奇怪。在这方面,每个人都经历了一个短暂的调整期。但是给它时间 - 过一会儿,你可能不希望再次看到逗号分隔的,带括号的无名参数列表。
In C++, you'd say something like:
在C ++中,你会说:
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.
你知道这些价值观是什么意思吗?其中有四个,显然参数是红色,绿色,蓝色,alpha顺序。还是阿尔法,红色,绿色,蓝色?当然,它也可能是色调,饱和度,价值,阿尔法......好吧,它并不重要,因为你总能看到它。
In Objective-C, you say:
在Objective-C中,你说:
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.
那不是更好吗?你肯定还需要不时查阅文档,以提醒自己确切的方法是什么,或者一个类提供什么方法。但是你不会经常发现自己正在查阅文档只是为了弄清楚哪个参数在哪里。
#3
1
Objective C has a descriptive way of writing methods..
Objective C有一种描述性的编写方法。
-(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
它是一个方法tableView,返回一个NSInteger,接受两个参数 - 一个UITableView引用和一个整数部分。现在考虑将numberofRowsInSection描述为参数的全部内容。看看这个例子
-(NSInteger) calculateSum:(NSInteger)operand1 secondOperand:(NSInteger)operand2 andThirdOperand:(NSInteger)operand3{}
and I can call this methods as [self calculateSum:var1 secondOperand:var2 andThirdOperand:var3];
我可以将此方法称为[self calculateSum:var1 secondOperand:var2 andThirdOperand:var3];
Here "secondOperand" and "andThirdOperand" are not essential, I can write the above method as
这里“secondOperand”和“andThirdOperand”不是必不可少的,我可以把上面的方法写成
-(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..
另请参阅我使用单词方法而不是通常是客观方式的函数。
#4
0
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
(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:
.
这是一个接受2个参数的方法,UITableView和NSInteger。它的注释如下:-tableView:numberOfRowsInSection:。
#5
0
Yep that's basically it. In obj-C the full method names include the arg names. I believe that convention originated from Smalltalk.
是的,基本上就是这样。在obj-C中,完整的方法名称包括arg名称。我认为该惯例源于Smalltalk。