Best way to handle this error? [AVCaptureFigVideoDevice setTorchModeOnWithLevel:error:]: unrecognized selector sent to instance

StackOverflow https://stackoverflow.com/questions/16288521

Question

I'm trying to turn off an image in my torch app if the device doesn't support LED dimming.

  NSError* outError;
        BOOL success = [device setTorchModeOnWithLevel:brightnessLevel error:&outError];
        if(!success){
            [self.lightDialIndicator setHidden: YES];
            self.lightDial.image = [UIImage imageNamed:@"light_dial_disabled.png"];
        }

but my app crashed with the following error

[AVCaptureFigVideoDevice setTorchModeOnWithLevel:error:]: unrecognized selector sent to instance 0x73ad460

Any idea of a better/working way of detecting when the device doesn't allow me to use setTorchModeOnWithLevel?

Was it helpful?

Solution

First off, setTorchModeOnWithLevel is a property on the AVCaptureDevice class.

Second, if you want to test if a class can respond to a certain selector that you're calling on it, you use this:

BOOL isSuccessful = NO;
if ([device respondsToSelector:@selector(setTorchModeOnWithLevel:error:)]) {
    NSError* outError;
    isSuccessful = [device setTorchModeOnWithLevel:brightnessLevel error:&outError];
}
if (!isSuccessful) {
    [self.lightDialIndicator setHidden: YES];
     self.lightDial.image = [UIImage imageNamed:@"light_dial_disabled.png"];
}

You didn't show how you instantiated device in your example, but this applies to any class where you're unsure of whether it has a certain method or not.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top