Learn Courses My Dashboard

Cannot convert value of type '(String) -> ()' to expected argument type 'T.Type'

hi CWC team,

I’m trying to Make a GET API call but I’m getting an error that I cannot find the fix in google,

image

this works perfectly in the module 6 (yelp app) but not working for my call for some reason.

has anybody faced this issue? if so, how did you fix it?
thanks

Here is my viewModel

//
//  ContentModel.swift
//  Secret
//
//  Created by Nat-Serrano on 8/11/21.
//

import Foundation

class ContentModel: ObservableObject, Identifiable {
    
    @Published var faces = [Face]() //to host the data pulled from the api FACEDETECT PIXLAB
    
    //func face detect
    func faceDetect(urlPath: String){
        
        //create url
        let urlString = Constants.pixLabapiURL + "facedetect?img=\(urlPath)"
        let url = URL(string: urlString)
        
        //create URL request (GET)
        if let url = url { //to avoid it being empty
            // Create URL Request
            var request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 10.0)
            request.httpMethod = "GET"
            request.addValue(Constants.pixLabApiKey, forHTTPHeaderField: "Authorization")
            
            // Get URLSession
            let session = URLSession.shared
            
            // Create Data Task
            let dataTask = session.dataTask(with: request) { (data, response, error) in
                
                // Check that there isn't an error
              if error == nil {
                
                    do {
                        let decoder = JSONDecoder()
                        let result = try decoder.decode(faceDetect.self, from: data!)
                        
                        
                        DispatchQueue.main.async { //assign things to properties from a background thread.NO GOOD PRACTICE use 
                            
                            self.faces = result
                        }
                       
                    }
                    catch {
                        print(error) //error from do block
                    }
                }
            }
        }
        
    }
    //func mogrify
    
    //func nsfw
    
    
    
}

here is my model

import Foundation


struct Face: Decodable {
    var face_id: Int
    var bottom: Int
    var right: Int
    var top: Int
    var left: Int
    var width: Int
    var height: Int
}

struct faceDetect: Decodable {
    var faces = [Face]() //response is an array of faces
}

this is the api
https://pixlab.io/cmd?id=facedetect

1 Like

Rename your faceDetect type to something else; the compiler is getting confused since you also have a function called faceDetect.

The easiest fix is to rename your faceDetect struct to FaceDetect, which you should do anyway since the convention in Swift is for types to start with a capital letter.

Thank you!

I hope someday I’ll become as good as you’re programming

1 Like

Yeah, roosterboy is a BOSS

Thanks for posting the question. Got me excited, when I saw FaceDetect! I’m still going through the Foundations course module 4 with the Recipe app, so it’s encouraging to see what’s in store later :slight_smile: