objective-c Cocoa @selector 用法说明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/543161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:07:53  来源:igfitidea点击:

Explanation of Cocoa @selector usage

objective-c

提问by mshafrir

I'm new to Cocoa/Cocoa Touch, and working through a development book. I've come across situations where the @selector() operator is used. I'm a bit lost on how and when the @selector() operator should be used. Can someone provide a short and sweet explanation and example of why it's used and what benefit it gives the developer?

我是 Cocoa/Cocoa Touch 的新手,正在阅读一本开发书籍。我遇到过使用 @selector() 运算符的情况。我对如何以及何时使用 @selector() 运算符有点迷茫。有人可以提供一个简短而甜蜜的解释和示例,说明为什么使用它以及它给开发人员带来什么好处?

By the way, here is sample code taken from Apple's iPhone development site that uses @selector()

顺便说一下,这里是从使用@selector() 的 Apple 的 iPhone 开发站点获取的示例代码

if ([elementName isEqualToString:@"entry"])
{

    parsedEarthquakesCounter++;

    // An entry in the RSS feed represents an earthquake, so create an instance of it.
    self.currentEarthquakeObject = [[Earthquake alloc] init];
    // Add the new Earthquake object to the application's array of earthquakes.
    [(id)[[UIApplication sharedApplication] delegate]
            performSelectorOnMainThread:@selector(addToEarthquakeList:)
            withObject:self.currentEarthquakeObject waitUntilDone:YES];
    return;
}

回答by Andrew Grant

The selector operator provides a way to refer to a method provided by an object, somewhat similar to a function pointer in C. It is useful because it allows you to decouple the process of calling methods on an object. For example one piece of code could provide a method, and another piece of code could apply that method to a given set of objects.

选择器运算符提供了一种引用对象提供的方法的方法,有点类似于 C 中的函数指针。它很有用,因为它允许您分离调用对象方法的过程。例如,一段代码可以提供一个方法,另一段代码可以将该方法应用于给定的一组对象。

Examples:

例子:

Test to see if an object implements a certain method:

测试一个对象是否实现了某个方法:

[object respondsToSelector:@selector(methodName)]

Store a method to later call on an object;

存储一个方法以供以后调用对象;

SEL method = @selector(methodName);
[object performSelector:method];

Call a method on a different thread (useful for GUI work).

在不同的线程上调用方法(对 GUI 工作很有用)。

[object performSelectorOnMainThread:@selector(methodName)]

回答by Brad Larson

In addition to what's been said, you can also wrap up the @selector in an NSInvocation for later use. You can set the arguments to the NSInvocation a long time after it's created, and activate it when you need the message to be fired. This gives you a lot of power.

除了上面所说的,您还可以将 @selector 包装在 NSInvocation 中以供以后使用。您可以在创建 NSInvocation 很长时间后为其设置参数,并在需要触发消息时激活它。这给了你很大的力量。

For an introduction to the concept, Scott Stevenson has a great post entitled "Dynamic Objective-C with NSInvocation".

关于这个概念的介绍,Scott Stevenson 有一篇很棒的文章,标题为“Dynamic Objective-C with NSInvocation”

回答by Petteri Hietavirta

One practical example is validateMenuItemmethod where menu items are identified with their target actions.

一个实际的例子是validateMenuItem方法,其中菜单项用它们的目标操作来标识。

Simplified example:

简化示例:

- (BOOL)validateMenuItem:(NSMenuItem *)item {
    if ([item action] == @selector(selectFiles:) && otherCondition) {
        return YES;
    } else {
        return NO;
    }
}

回答by mouviciel

@selector()is used each time you need to pass the name of a method as an argument to another method, a function or as a variable value. Passing directly the name doesn't work in objective-C.

@selector()每次需要将方法名称作为参数传递给另一个方法、函数或作为变量值时,都会使用它。直接传递名称在 Objective-C 中不起作用。

回答by morroko

You can use a selector to invoke a method on an object—this provides the basis for the implementation of the target-action design pattern in Cocoa.

您可以使用选择器来调用对象上的方法——这为在 Cocoa 中实现目标-动作设计模式提供了基础。

[myObject performSelector:@selector(runMYmethod:) withObject:parameters];

is equivalent to:

相当于:

[myObject runMYmethod:parameters];