[ad_1]
I’m a newbie in iOS growth. I used to be making an attempt to make use of an api URl: https://www.arbeitnow.com/api/job-board-api in my job search iOS app. However nothing exhibits on my app. I examined the URL in POSTMAN and it returns json(however HTML in description half?). I wrote the code:
func getResults(accomplished: @escaping (Outcome<[Results], ErrorMessage>) -> Void) {
let urlString = "https://www.arbeitnow.com/api/job-board-api"
guard let url = URL(string: urlString) else {return}
let activity = URLSession.shared.dataTask(with: url) { (knowledge, response, error) in
if let _ = error {
accomplished(.failure(.invalidData))
return
}
guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {
accomplished(.failure(.invalidResponse))
return
}
guard let knowledge = knowledge else {
accomplished(.failure(.invalidData))
return
}
do {
let deconder = JSONDecoder()
deconder.keyDecodingStrategy = .convertFromSnakeCase
let outcomes = attempt deconder.decode([Results].self, from: knowledge)
accomplished(.success(outcomes))
} catch {
accomplished(.failure(.invalidData))
}
}
activity.resume()
}
I used the code in HomeViewController:
override func viewDidLoad() {
tremendous.viewDidLoad()
title = "Dwelling"
collectionView.backgroundColor = UIColor(named: "backgroundMain")
collectionView.register(SearchViewCell.self, forCellWithReuseIdentifier: cellId)
setupSearchBar()
Service.shared.getResults() {[weak self] lead to
swap consequence {
case .success(let outcomes):
print(outcomes)
self?.jobResults = outcomes
DispatchQueue.essential.async {
self?.collectionView.reloadData()
}
case .failure(let error):
print(error)
}
}
}
I do not know what’s unsuitable with my code. Can anybody assist? Thanks!
[ad_2]
