Home > other >  Swift error saying it cannot assign to property due to it being a 'let' constant
Swift error saying it cannot assign to property due to it being a 'let' constant

Time:02-02

Here is the snippet of the code:

public var captureDevice: CaptureDevice?

...

guard let captureDeviceTest = AVCaptureDevice.DiscoverySession.init(
            deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera],
            mediaType: AVMediaType.video,
            position: cameraPosition).devices.first else {
                return
        }
        
self.captureDevice = captureDeviceTest
            
if let captureDevice = self.captureDevice {

   ...

   // This is where error occurs
   if captureDevice.isExposureModeSupported(.continuousAutoExposure) {
         captureDevice.exposureMode = .continuousAutoExposure
    }

The error I am getting is: Cannot assign to property: 'captureDevice' is a 'let' constant. I tried suggestion here to use inout but despite placing that in various places, it still didn't work for me.

Any swift experts can help me out here?

CodePudding user response:

You are accessing the local variable captureDevice, not the struct's captureDevice variable.

Just replace the line causing the error to the following:

self.captureDevice?.exposureMode = .continuousAutoExposure

Note the self. before captureDevice.

You might think of changing the conditional binding to using var instead of let, but that won't actually work. This is because you are just mutating a local copy. It will fix the compile error but the logic won't work.

  •  Tags:  
  • Related