Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Data return issue in swiftUI from another function

Below is my code which is calling an API and performing action as per the code

import Foundation

class apiCall {
    func getUsers(completion:@escaping ([Result]) -> ()) {
        
        let url = URL(string: "http://myapi-call-ges-here")!
        var request = URLRequest(url: url)
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        request.addValue("application/json", forHTTPHeaderField: "Accept")
        request.httpMethod = "GET"

        URLSession.shared.dataTask(with: request) { (data, response , error) in
     
            do{
                let result = try JSONDecoder().decode(Result.self,from: data!)
                print(result)
                
            }
            catch{
                print("Error:\(error)")
            }
         }.resume()
        }
}

But I want to do code clean up and wanted to keep API calling code in different file as shown below

import Foundation

class apiCall {
    func getUsers(completion:@escaping ([Result]) -> ()) {

      let request=apiParamfunc()

        URLSession.shared.dataTask(with: request) { (data, response , error) in

            do{
                let result = try JSONDecoder().decode(Result.self,from: data!)
                print(result)

            }
            catch{
                print("Error:\(error)")
            }
         }.resume()
        }
}

apiParamfunc is a function which is created in another file

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import Foundation
 funct apiParamfunc-> Any{

     let url = URL(string: "http://myapi-call-ges-here")!
        var request = URLRequest(url: url)
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        request.addValue("application/json", forHTTPHeaderField: "Accept")
        request.httpMethod = "GET"
   
       return request


}

but I am getting an error for dataTask as "No exact matches in call to instance method ‘dataTask’ ". I even tried with different datatype but not getting any helpful thing,please help me on this

>Solution :

You cannot use Any, but specify exact type, like

 funct apiParamfunc() -> URLRequest {     // << here !!

     let url = URL(string: "http://myapi-call-ges-here")!
     var request = URLRequest(url: url)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading