Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to define an anonymous selector in Objective-C?

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.

like image 324
Ben S Avatar asked Nov 23 '09 00:11

Ben S


1 Answers

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.

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.

like image 156
Chuck Avatar answered Sep 18 '22 06:09

Chuck