[ad_1]
Beneath is my code which is asking an API and performing motion as per the code
import Basis
class apiCall {
func getUsers(completion:@escaping ([Result]) -> ()) {
let url = URL(string: "http://myapi-call-ges-here")!
var request = URLRequest(url: url)
request.addValue("software/json", forHTTPHeaderField: "Content material-Sort")
request.addValue("software/json", forHTTPHeaderField: "Settle for")
request.httpMethod = "GET"
URLSession.shared.dataTask(with: request) { (information, response , error) in
do{
let outcome = strive JSONDecoder().decode(Consequence.self,from: information!)
print(outcome)
}
catch{
print("Error:(error)")
}
}.resume()
}
}
However I wish to do code clear up and wished to maintain API calling code in several file as proven beneath
import Basis
class apiCall {
func getUsers(completion:@escaping ([Result]) -> ()) {
let request=apiParamfunc()
URLSession.shared.dataTask(with: request) { (information, response , error) in
do{
let outcome = strive JSONDecoder().decode(Consequence.self,from: information!)
print(outcome)
}
catch{
print("Error:(error)")
}
}.resume()
}
}
apiParamfunc is a perform which is created in one other file
import Basis
funct apiParamfunc-> Any{
let url = URL(string: "http://myapi-call-ges-here")!
var request = URLRequest(url: url)
request.addValue("software/json", forHTTPHeaderField: "Content material-Sort")
request.addValue("software/json", forHTTPHeaderField: "Settle for")
request.httpMethod = "GET"
return request
}
however I get an error for dataTask as “No precise matches in name to occasion methodology ‘dataTask’ “. I even tried with totally different datatype however not getting any useful factor,please assist me on this
[ad_2]
