有人可以解释这个 Objective-C 方法声明语法吗 [英] Can somebody explain this Objective-C method declaration syntax
问题描述
我正在阅读一本 iPhone 开发书*,但并不真正了解 Objective C.在大多数情况下,我能够了解正在发生的事情,但有一些方法声明如下所示,我有一个解析有点麻烦.例如:
I'm working through an iPhone development book* without really knowing Objective C. For the most part I'm able to follow what's going on, but there are a few method declarations like the one below that I'm having a bit of trouble parsing. For example:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
return [self.controllers count]; //controllers is an instance variable of type NSArray in this class
}
看起来这是一个名为numberOfRowsInSection的方法,它返回一个NSInteger,并以一个NSInteger作为参数,在本地称为'section'.但我不明白所有对 tableView 的引用,或者为什么当它没有在方法中使用时需要一个参数.有人可以澄清这一点吗?谢谢.
It looks this is a method called numberOfRowsInSection, and it returns an NSInteger, and takes an NSInteger as a parameter which is locally called 'section'. But I don't understand all the references to tableView, or why this takes a parameter when it is not used within the method. Can somebody clarify this? Thanks.
*p.258,开始 iPhone 3 开发,Mark 和 LaMarche,Apress 出版
*p. 258, Beginning iPhone 3 Development, by Mark and LaMarche, published by Apress
更新:我找到了另一个更详细的 SO 线程:Objective C 中的方法语法
Update: I was able to find another SO thread that goes into a bit more detail: Method Syntax in Objective C
推荐答案
这是一个方法叫做:
tableView:numberOfRowsInSection:
它需要两个参数:
- a
UITableView*
- 一个
NSInteger
该方法还采用一个隐式的 self
参数,即调用它的实例.正如 Dreamlax 所指出的,它还需要一个隐式 _cmd
,这是当前被调用的方法.
The method also takes an implicit self
parameter , which is the instance it is called with. As dreamlax notes, it also takes an implicit _cmd
, which is the method that currently gets invoked.
正如 Mark 所说,如果您符合某个接口,则不使用某些参数是完全常见的.
As Mark says, it is completely common to not use certain parameters if you are conforming to a certain interface.
这篇关于有人可以解释这个 Objective-C 方法声明语法吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!