1

I would like to be able to execute a piece of code that is defined in a string. I am aware of performSelector: but the object that will perform the selector is going to be different.

Example strings

[[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo] hasFlash]
[UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]

So what I would like to do is something along the lines of

SEL selector = NSSelectorFromString(@"[[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo] hasFlash]");
if (selector) {
    // Show flash buttons
}
user2123173
  • 118
  • 1
  • 5
  • 1
    Sorry, not possible: http://stackoverflow.com/questions/10158036/eval-in-objective-c – fguchelaar Mar 01 '13 at 10:50
  • 1
    Just think of it for a while: Isn't objective-c a compiled language? Does your phone have a compiler for this? Let's think about this a bit more: wouldn't this create a security issue? – ppeterka Mar 01 '13 at 10:50

1 Answers1

2

You can not fire a selector that calls a nested method call.

Selectors are only method names with showing number of arguments as method:abc:yxa:

As the statement below:

SEL selector = NSSelectorFromString(@"[[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo] hasFlash]");

is calling

[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]

then

[objectReturnedByAbove hasFlash]
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • I understand. I would be able to by breaking down the strings but then there are better solutions for this problem. Thanks! – user2123173 Mar 04 '13 at 09:05
  • few weeks ago, i need similar kind of thing, i posted and got a lot downvotes and question got removed. You should thank to everyone as your question is still here :D – Anoop Vaidya Mar 04 '13 at 09:07