Lander Saavedra
Lander Saavedra

Reputation: 25

Cannot convert value of type '(_) -> ()' to expected argument type '((Bool, Error?) -> Void)?'

Please, I need help with are problem, I changing the syntax Swift 3 for swift 4 and now i have many problems for identification of all my bugs.The Error its on the function savePhoto in the last line., completionHandler: { _ in

func takePhoto(_ previewLayer: AVCaptureVideoPreviewLayer, location: CLLocation?, completion: (() -> Void)? = nil) {
    guard let connection = stillImageOutput?.connection(with: AVMediaType.video) else { return }

    connection.videoOrientation = Helper.videoOrientation()

    queue.async {
      self.stillImageOutput?.captureStillImageAsynchronously(from: connection) {
        buffer, error in

        guard let buffer = buffer, error == nil && CMSampleBufferIsValid(buffer),
          let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer),
          let image = UIImage(data: imageData)
          else {
            DispatchQueue.main.async {
              completion?()
            }
            return
        }

        self.savePhoto(image, location: location, completion: completion)
      }
    }
  }

  func savePhoto(_ image: UIImage, location: CLLocation?, completion: (() -> Void)? = nil) {
    PHPhotoLibrary.shared().performChanges({
      let request = PHAssetChangeRequest.creationRequestForAsset(from: image)
      request.creationDate = Date()
      request.location = location
    }, completionHandler: { _ in
        DispatchQueue.main.async {
          completion?()
        }
    })
  }

Upvotes: 1

Views: 2612

Answers (2)

vadian
vadian

Reputation: 285082

As you can clearly see in the error message the completionHandler passes two parameters rather than just one.

So you have to write

}, completionHandler: { (_, _) in

but you are strongly encouraged to handle the result and a potential error. Ignoring errors causes bad user experience.

Upvotes: 0

Milan Nosáľ
Milan Nosáľ

Reputation: 19737

Rewrite it to:

}, completionHandler: { (_, _) in

As per documentation, completion handler in performChanges(_:completionHandler:) accepts two parameters, not just one. _ in, what you have used, is a placeholder for a single parameter only.

Upvotes: 1

Related Questions