Swift : Error on value of type 'Data' to type 'Data'

swift error types
swift error localizeddescription
swift error code
swift error debugdescription
'catch' block is unreachable because no errors are thrown in 'do' block
swift 4 create error object
thrown expression type 'string' does not conform to 'error'
swift nserror

I have some function for Camera using AVKit and AVCapturePhotoCaptureDelegate.

import UIKit 
import AVKit

class CaptureImageClass: NSObject, AVCapturePhotoCaptureDelegate {

    var photoData: Data?

    func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {

        if let error = error {
            print("Error capturing photo: \(error)")
        } else {
            photoData = photo.fileDataRepresentation() //Cannot assign value of type 'Data?' to type 'Data?'
        }
    }

    func capture(_ output: AVCapturePhotoOutput, didFinishCaptureForResolvedSettings resolvedSettings: AVCaptureResolvedPhotoSettings, error: Error?) {
        guard let photoData = photoData else {
            print("No photo data resource")
            return
        }
        let capturedImage = UIImage.init(data: photoData , scale: 1.0) //Cannot convert value of type 'Data' to expected argument type 'Data'
        if let image = capturedImage {
            UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
        }
    }
}

this code compiled without problem when i make standalone project. But when i try to apply to another project, it have some error like Cannot assign value of type 'Data?' to type 'Data?' and Cannot convert value of type 'Data' to expected argument type 'Data' Is this problem caused by diffrent Swift version or not?

Note: this "another project" deployment target is iOS 10 & swift 3 and using func capture for didFinishCaptureForResolvedSettings and cant using func photoOutput My standalone project is running using Swift 4 and for the deployment target is iOS 11.3

Check if the error is not caused by the iOS version of your device.

Keep in mind that

func photoOutput(_ output: AVCapturePhotoOutput, 
didFinishProcessingPhoto 
photo: AVCapturePhoto, 
error: Error?) {

Is available for iOS11 and later for iOS10 and earlier you should use

optional func photoOutput(_ output: AVCapturePhotoOutput,
 didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?,
previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?,
resolvedSettings: AVCaptureResolvedPhotoSettings, 
bracketSettings: AVCaptureBracketedStillImageSettings?, 
error: Error?)

Also

fileDataRepresentation()

Is available for iOS11 and later

Hope it helps...

Error Handling, It expects Error? whereas you have used NSError? . Try (data : CMPedometerData?, error: Error?) -> Void. There are three kinds of variable declaration in swift programming. You can declare a swift variable as a variable (var name: String = "jerry ") that value can be changed in code, a constant ( let x:Int = Int(10)) that value can not be changed, and an optional variable ( var x: Int?) value can be changed and do not need to be initialized.

Someone has override the Data type. You can bypass this using :

var photoData: Swift.Data?

Cannot convert value of type error in Swift, Optionals as error indicators. For simple scenarios you can always use optional values, to indicate that something bad happened. Also the guard  The problem. When performing many kinds of operations, it's very common to have two distinct outcomes — success and failure.In Objective-C, those two outcomes were usually modeled by including both a value and an error, when — for example — calling a completion handler once the operation finished.

Solved by using func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {

    // Make sure we get some photo sample buffer
    guard error == nil,
        let photoSampleBuffer = photoSampleBuffer else {
            print("Error capturing photo: \(String(describing: error))")
            return
    }
    // Convert photo same buffer to a jpeg image data by using // AVCapturePhotoOutput
    guard let imageData =
        AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer) else {
            return
    }

    let dataProvider = CGDataProvider(data: imageData as CFData)

    let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.absoluteColorimetric)


    let image = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.right)

    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}

How to use the result type to handle errors in Swift 5?, One big benefit of Swift's type system is how it lets us eliminate a lot of enum Result<Value> { case success(Value) case failure(Error) }. Error handling is the process of responding to and recovering from error conditions in your program. Swift provides first-class support for throwing, catching, propagating, and manipulating recoverable errors at runtime. Some operations aren’t guaranteed to always complete execution or produce a useful output.

The power of Result types in Swift, How to fix “Fatal error: No ObservableObject of type SomeType found” value of type 'String' to expected argument type 'Binding<String>'”. Returns a Boolean value indicating whether the value of the first argument is less than or equal to that of the second argument. static func == (Object Identifier, Object Identifier) -> Bool Returns a Boolean value indicating whether two values are equal.

How to fix “Cannot convert value of type 'String , Before Swift 5 we used to write our own Result type to contain a value or an error (but never both). A lot of 3rd party libraries brought along their  Type casting in Swift is implemented with the is and as operators. These two operators provide a simple and expressive way to check the type of a value or cast a value to a different type. You can also use type casting to check whether a type conforms to a protocol, as described in Checking for Protocol Conformance.

Swift 5's Result Type, Error values in Swift are represented using value types that conform to the ErrorProtocol protocol (this was renamed from the  Hello all, I'm trying to display the result in a UITextField, but I'm getting the following error: Cannot convert value of type 'String?' to expected argument type '[Int]' I'm also new to swift, and I understand little bit about the issue, but unfortunatelly not enough to fix it. So, your help will be much appreciated. This is the code:

Comments
  • Do you have another type named Data defined somewhere?