是否可以在Objective-C中定义匿名选择器?

时间:2023-01-15 18:24:49

I'd like to be able to define an inline anonymous selector that a selector wherever a selector is needed as an argument.

我希望能够定义一个内联匿名选择器,选择器只需要一个选择器作为参数。

Is this possible, or do I have to just suck it up and define a method?

这是可能的,还是我必须吮吸它并定义一个方法?

Background: In my iPhone application I need to update my UI from another thread. To do this I use performSelectorOnMainThread:withObject:waitUntilDone: However, I'd like to be able to get this functionality without having to define a whole other method.

背景:在我的iPhone应用程序中,我需要从另一个线程更新我的UI。为此,我使用performSelectorOnMainThread:withObject:waitUntilDone:但是,我希望能够获得此功能而无需定义其他整个方法。

1 个解决方案

#1


16  

Unfortunately, no. The idea is self-contradictory — a selector is a name. That's all it is. It doesn't define any functionality.

很不幸的是,不行。这个想法是自相矛盾的 - 选择器是一个名字。就是这样。它没有定义任何功能。

Objective-C didn't have any kind of anonymous function until just recently when blocks were introduced into Mac OS X. It's possible to use them on the iPhone through Plausible Blocks, but they're still not integrated into the APIs there.

直到最近,当块被引入Mac OS X时,Objective-C没有任何类型的匿名功能。可以通过Plausible Blocks在iPhone上使用它们,但它们仍然没有集成到那里的API中。

Update 2014

This answer was correct back in 2009, but by now Apple has integrated blocks very well into the iOS frameworks. They're used pretty pervasively for callbacks now and are heavily used in the Grand Central Dispatch concurrency library.

这个答案在2009年是正确的,但到目前为止,Apple已经将块很好地集成到了iOS框架中。它们现在非常普遍地用于回调,并且在Grand Central Dispatch并发库中被大量使用。

#1


16  

Unfortunately, no. The idea is self-contradictory — a selector is a name. That's all it is. It doesn't define any functionality.

很不幸的是,不行。这个想法是自相矛盾的 - 选择器是一个名字。就是这样。它没有定义任何功能。

Objective-C didn't have any kind of anonymous function until just recently when blocks were introduced into Mac OS X. It's possible to use them on the iPhone through Plausible Blocks, but they're still not integrated into the APIs there.

直到最近,当块被引入Mac OS X时,Objective-C没有任何类型的匿名功能。可以通过Plausible Blocks在iPhone上使用它们,但它们仍然没有集成到那里的API中。

Update 2014

This answer was correct back in 2009, but by now Apple has integrated blocks very well into the iOS frameworks. They're used pretty pervasively for callbacks now and are heavily used in the Grand Central Dispatch concurrency library.

这个答案在2009年是正确的,但到目前为止,Apple已经将块很好地集成到了iOS框架中。它们现在非常普遍地用于回调,并且在Grand Central Dispatch并发库中被大量使用。