1 year ago

#77546

test-img

John Gerard

Cannot convert value of type 'Model' to expected argument type 'Data'

I'm trying to set up a function to capture the response from an API request and map it to a Model once the result hits.

I'm getting this error on the "data" in the "let "cloudinaryResponse" line": Cannot convert value of type 'CLDUploadResult' to expected argument type 'Data'.

The function works fine if I don't do anything with the result. The result that's returned is of Model type "CloudinaryUploadResponse" so I know it'll match.

    private func uploadPhoto(image: UIImage){
        let config = CLDConfiguration(cloudName: "my_sample_cloud", secure: false)
        let cloudinary = CLDCloudinary(configuration: config)
        
        if let image = image.pngData() {
            cloudinary.createUploader().upload(
                data: image, uploadPreset: "my_sample_preset") { progress in
                  print(progress)
            } completionHandler: { result, error in
                
                guard let data = result else { return }
                do {
                 let cloudinaryResponse = try JSONDecoder().decode(CloudinaryUploadResponse.self, from: data)
                 print(cloudinaryResponse)
                } catch {
                    print(error)
                }
            }

        }
    }

I get the same error if I replace "data" with CLDUploadResult

Here's the model I'm trying to map the result to:

struct CloudinaryUploadResponse: Codable {
    let assetID, publicID: String
    let version: Int
    let versionID, signature: String
    let width, height: Int
    let format, resourceType: String
    let createdAt: Date
    let tags: [String]
    let bytes: Int
    let type, etag: String
    let placeholder: Bool
    let url: String
    let secureURL: String
    let accessMode: String
    let existing: Bool
    let originalFilename: String

    enum CodingKeys: String, CodingKey {
        case assetID = "asset_id"
        case publicID = "public_id"
        case version
        case versionID = "version_id"
        case signature, width, height, format
        case resourceType = "resource_type"
        case createdAt = "created_at"
        case tags, bytes, type, etag, placeholder, url
        case secureURL = "secure_url"
        case accessMode = "access_mode"
        case existing
        case originalFilename = "original_filename"
    }
}

swift

swiftui

cloudinary

0 Answers

Your Answer

Accepted video resources