user2123173
user2123173

Reputation: 118

Executing code defined in a string

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
}

Upvotes: 1

Views: 274

Answers (1)

Anoop Vaidya
Anoop Vaidya

Reputation: 46563

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]

Upvotes: 2

Related Questions