Search code examples
iosswiftoption-typeweak

Cannot assign value type mismatch swift


I have the following code snippet.

 camera.onDeviceChange = { [weak self] (camera: LLSimpleCamera!, device: AVCaptureDevice!) -> Void in
      print("Device changed.")
 }

This used to work fine in Swift 2, but now I am getting the following error message:

Cannot assign value of type '(LLSimpleCamera!, AVCaptureDevice!) -> Void' to type '((LLSimpleCamera?, AVCaptureDevice?) -> Void)!'

Not really sure how to change this, I tried matching the type by changing the ! to optionals and then adding ! after the void, however this didn't work.


Solution

  • Your error suggest type mismatch that means LLSimpleCamera! != LLSimpleCamera? .. there is no need to define type. .. you can use it something like

    camera.onDeviceChange = { [weak self] (camera, device) -> Void in
       print("Device changed.")
    }