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
}
2
  • 1
    Sorry, not possible: stackoverflow.com/questions/10158036/eval-in-objective-c Commented Mar 1, 2013 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? Commented Mar 1, 2013 at 10:50

1 Answer 1

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]
Sign up to request clarification or add additional context in comments.

2 Comments

I understand. I would be able to by breaking down the strings but then there are better solutions for this problem. Thanks!
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.