无法将类型“(_) -> ()”的值转换为预期的参数类型“((Bool, Error?) -> Void)?”
Cannot convert value of type '(_) -> ()' to expected argument type '((Bool, Error?) -> Void)?'
拜托,我需要帮助解决问题,我将语法 Swift 3 更改为 swift 4,现在我在识别所有 bugs.The 错误时遇到很多问题最后一行的 savePhoto 函数,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?()
}
})
}
改写为:
}, completionHandler: { (_, _) in
根据 documentation,performChanges(_:completionHandler:)
中的完成处理程序接受两个参数,而不仅仅是一个。 _ in
,你用的,只是一个单一参数的占位符。
正如您在错误消息中清楚看到的那样,completionHandler
传递了 两个 参数,而不是一个。
所以你必须写
}, completionHandler: { (_, _) in
但强烈建议您处理结果和潜在的错误。忽略错误会导致糟糕的用户体验。
拜托,我需要帮助解决问题,我将语法 Swift 3 更改为 swift 4,现在我在识别所有 bugs.The 错误时遇到很多问题最后一行的 savePhoto 函数,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?()
}
})
}
改写为:
}, completionHandler: { (_, _) in
根据 documentation,performChanges(_:completionHandler:)
中的完成处理程序接受两个参数,而不仅仅是一个。 _ in
,你用的,只是一个单一参数的占位符。
正如您在错误消息中清楚看到的那样,completionHandler
传递了 两个 参数,而不是一个。
所以你必须写
}, completionHandler: { (_, _) in
但强烈建议您处理结果和潜在的错误。忽略错误会导致糟糕的用户体验。