Код: Выделить всё
func getGenres(_ completion: @escaping (CompetitionGroup?) -> ()) {
let headers = [
"x-rapidapi-key": "api-key***",
"x-rapidapi-host": "football-web-pages1.p.rapidapi.com"
]
let request = NSMutableURLRequest(url: NSURL(string: "https://football-web-pages1.p.rapidapi.com/competitions.json?include=rounds")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
request.httpMethod = "GET"
request.allHTTPHeaderFields = headers
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error as Any)
} else {
let httpResponse = response as? HTTPURLResponse
do {
let decoder = JSONDecoder()
let comps = try decoder.decode(CompetitionGroup.self, from: data!)
self.competitions = comps
completion(self.competitions)
}
catch {
print("Error in JSON")
}
}
})
dataTask.resume()
}
Код: Выделить всё
private func setupUI() {
self.view.backgroundColor = .systemBackground
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(didTapLogout))
self.view.addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
label.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
label.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
])
getGenres { (array) in
// Do operation with array
competitionslist = array
let childView = UIHostingController(rootView: ContentView()) // ERROR HERE
self.addChild(childView)
childView.view.frame = self.view.bounds
self.view.addSubview(childView.view)
childView.didMove(toParent: self)
}
//competitionslist = getComps()
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... re-setupui
Мобильная версия