Objective-C 中的选择器只是向对象发送消息的另一种方式吗?
Objective-C 中的选择器只是向对象发送消息的另一种方式吗?我真的不明白为什么或如何使用它们。
Are selectors in Objective-C just another way to send a message to an object? I really don't understand why or how to use them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当您想要定义回调机制时,通常会使用选择器。 Cocoa 中选择器最常见的用例是控件,例如按钮。
UIButton
非常通用,因此不知道按下按钮时会发生什么。在使用它之前,您需要告诉它按下按钮时应该运行什么方法。操作如下:然后,当按下按钮时,按钮将调用我们传递给它的目标上的选择器。使用这种机制,您不需要每次希望按钮调用您自己的代码时都对其进行子类化。相反,
UIButton
本身具有用于分派到您选择的任何代码的通用机制。 (好吧,从技术上讲,是超类UIControl
提供了调度机制。)Selectors are usually used when you want to define a callback mechanism. The most common use case for selectors in Cocoa is with controls, such as buttons. A
UIButton
is very generic, and as such has no idea what should happen when the button is pressed. Before you can use one, you need to tell it what method should be run when the button is pressed. This is done as follows:Then, when the button is pressed, the button will call the selector on the target we passed it. With this mechanism, you don't need to subclass a button every time you want it to call some of your own code. Instead,
UIButton
itself has a generic mechanism for dispatching to any code you choose. (Okay, technically, it's the superclassUIControl
that's providing the dispatch mechanism.)它们不是向对象发送消息的另一种方式,而是唯一的方式。例如,在
[myView setValue:@"foo"]
中,setValue:
是一个选择器。 (编写相同内容的另一种不太方便的方法是 objc_msgSend(myView, @selector(setValue:), @"foo")。)正如 Ian Henry 所说,您可以使用
SEL< /code> 值在运行时而不是编译时选择选择器。这是 Cocoa 中的一项基本技术;用户界面通常使用目标/操作绑定连接到控制器,其中目标是对象,操作是选择器。通常,您可以在笔尖中进行设置,但也可以在代码中进行设置:
They aren’t another way to send a message to an object, they’re the only way. For example, in
[myView setValue:@"foo"]
,setValue:
is a selector. (Another, less convenient way of writing the same thing isobjc_msgSend(myView, @selector(setValue:), @"foo")
.)As Ian Henry says, you can use
SEL
values to choose a selector at runtime instead of compile time. This is a fundamental technique in Cocoa; user interfaces are generally connected to controllers using target/action bindings, where the target is an object and the action is a selector. Normally you set this up in a nib, but you can also do it in code:您可以将选择器存储为变量,并在以后或在不同的上下文中调用它们。例如,您可以告诉对象在特定时间或在不同线程上执行选择器。您还可以根据数据选择要执行的选择器,这就是界面构建器和核心数据的工作方式。
You can store selectors as variables, and invoke them later or in a different context. For example you can tell an object to perform a selector at a particular time, or on a different thread. You can also choose which selector to perform based on data, which is how interface builder and core data do their thing.
最基本的是,是的,但您可以在运行时更改消息。例如:
然后在
selectorFactory.getSelector
中:来自
C#
或其他类似语言,您可以使用它比使用更轻松地(松散地)模拟事件NSNotifications。例如,您可以创建一个具有两个 ivars(
target
和selector
)的按钮类,并让按钮在单击时在目标上执行选择器(例如)。不过,选择器的作用远不止这些。在这里阅读有关它们的更多信息:
http:// developer.apple.com/mac/library/documentation/cocoa/conceptual/objectivec/articles/ocSelectors.html
At the most basic, yes, but you can change the message at runtime. For example:
And then in
selectorFactory.getSelector
:Coming from
C#
or another similar language, you can use this to (loosely) simulate events much more easily than usingNSNotification
s. For example, you could make a button class with two ivars,target
andselector
, and have the button perform the selector on the target when it's clicked (for example).There's a lot more to selectors than that, though. Read more about them here:
http://developer.apple.com/mac/library/documentation/cocoa/conceptual/objectivec/articles/ocSelectors.html
--来自Apple Developer Library --
选择器是用于选择要为对象执行的方法的名称,或者是在编译源代码时替换该名称的唯一标识符。选择器本身不执行任何操作。它只是标识一个方法。选择器方法名称与普通字符串的唯一区别是编译器确保选择器是唯一的。选择器的有用之处在于(与运行时结合)它的作用就像一个动态函数指针,对于给定的名称,它自动指向适合与它一起使用的任何类的方法的实现。假设您有一个用于方法运行的选择器,以及类 Dog、Athlete 和 ComputerSimulation(每个类都实现了一个方法运行)。选择器可以与每个类的实例一起使用来调用其 run 方法 - 即使每个类的实现可能不同。
--From Apple Developer Library --
A selector is the name used to select a method to execute for an object, or the unique identifier that replaces the name when the source code is compiled. A selector by itself doesn’t do anything. It simply identifies a method. The only thing that makes the selector method name different from a plain string is that the compiler makes sure that selectors are unique. What makes a selector useful is that (in conjunction with the runtime) it acts like a dynamic function pointer that, for a given name, automatically points to the implementation of a method appropriate for whichever class it’s used with. Suppose you had a selector for the method run, and classes Dog, Athlete, and ComputerSimulation (each of which implemented a method run). The selector could be used with an instance of each of the classes to invoke its run method—even though the implementation might be different for each.