Objective-C 方法的语义
来自 Java 背景的我仍然不太理解 Objective-C 方法的语义,而不是它们的语法。以以下方法为例:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
很简单。有一个名为 numberOfSectionsInTableView 的方法,它采用 UITableView 作为参数并返回 NSInteger。现在,这些方法怎么样:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
我应该如何解释这些方法?我偶然发现了三种可能的解释:
1)所有三个方法都被命名为tableView,并且它们重载了不同的参数。当人们谈论“调用 numberOfRowsInSection 方法”时,这似乎不太可能。
2) 这些方法被命名为numberOfRowsInSection、heightForRowAtIndexPath和cellForRowAtIndexPath,并且tableView是由于委托而产生的工件。在这种情况下,tableView 部分在语法上到底意味着什么?如果方法的格式是“(return)name:parameters,tableView适合在哪里?
3)由于Objective-C使用消息传递,所以考虑方法是错误的。而是考虑直接将消息传递给对象。换句话说,如果对象接收到名为 tableView 和 numberOfRowsInSection 的消息,它就知道要执行代码的某一部分。如果是这种情况,顺序重要吗?传递numberOfRowsInSection和tableView与传递tableView和numberOfRowsInSection相同吗?
Coming from a Java background, I'm still don't quite understand the semantics of Objective-C methods as opposed to their syntax. Take as an example the following method:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
Easy enough. There is a method named numberOfSectionsInTableView which takes a UITableView as a parameter and returns a NSInteger. Now, how about these methods:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
How should I interpret these methods? I've stumbled upon three possible explanations:
1) All three the methods are named tableView and they are overloaded with different parameters. This seems unlikely as people talk about 'calling the numberOfRowsInSection method'.
2) The methods are named numberOfRowsInSection, heightForRowAtIndexPath and cellForRowAtIndexPath, and the tableView is an artifact due to delegation. In that case, what exactly does the tableView part mean syntactically? If the format of a method is '(return)name:parameters, where does tableView fit in?
3) As Objective-C uses message passing, it's wrong to think about methods. Rather think about passing messages to the object directly. In other words, if the object receives the messages named tableView and numberOfRowsInSection, it knows to execute a certain part of the code. If this is the case, does order matter? Is passing numberOfRowsInSection and tableView the same as passing tableView and numberOfRowsInSection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
方法的名称(或用 Objective-C 的说法是选择器)包括名称的所有部分。例如,看看这三个方法,它们都可以在同一个类中共存:
注意带有参数的“foo”版本名称中的冒号:这是至关重要的。方法采用的参数数量是其名称的一部分。这实际上是严格定义的,因为有时在 Objective-C 中您需要引用方法的完整选择器(使用
@selector
指令)。请参阅选择器部分Objective-C 编程语言文档。此外,订单也很重要。这两种方法的不同之处在于:
Objective-C 中没有重载。编译器不会分派具有相同名称但参数类型不同的不同方法。
示例中的前导
tableView:
只是用于委托的常见命名约定;所有委托选择器都将以一个参数开头,该参数指定委托功能的对象。The name (or selector, in Objective-C's parlance) of a method includes all of the parts of the name. For example, look at these three methods, all of which can coexist in the same class:
Note the colon in the name of the version of "foo" that takes a parameter: that's crucial. The number of parameters that a method takes is part of its name. This is actually strictly defined, because at times in Objective-C you need to refer to the full selector of a method (using the
@selector
directive). See the Selectors section of the Objective-C Programming Language document.Additionally, the order is significant. These two methods are different:
There is no overloading in Objective-C. The compiler won't dispatch to different methods with the same name but different parameter types.
The leading
tableView:
in your examples is simply a common naming convention used for delegates; all delegate selectors will start with a parameter that specifies the object that is delegating functionality.在 Objective-C 中,方法名称被称为 签名 和方法通常称为选择器。
您的示例中四个选择器的签名是。
cellForRowAtIndexPath:tableView:
与tableView:cellForRowAtIndexPath:
不同每个类不能有多个具有相同签名的选择器。
In Objective-C the method names are referred to as signatures and the methods are often called selectors.
The signature of the four selectors in your example are.
cellForRowAtIndexPath:tableView:
is not the same astableView:cellForRowAtIndexPath:
There cannot be more than one selector with the same signature per class.
在 Objective-C 中,参数散布在方法名称中(称为“选择器”),而不是作为名称后面括号中的列表传递。完整的选择器就是那个很长的东西(tableView:numberOfRowaInSection :)。选择器在冒号处分开,这就是放置参数的地方。每个冒号都有一个参数。不带任何冒号的选择器表示不带参数的方法。
In Objective-C, the arguments are interspersed with the method name (called the "selector") instead of passed as a list in parentheses after the name. The full selector is that whole long thing (tableView:numberOfRowaInSection:). The selector is split at the colons and that's where the arguments are placed. There is one argument for each colon. A selector without any colons signifies a method that takes no parameters.