Я делаю этот пример на детской площадке
import UIKit
import PlaygroundSupport
// MARK: - ProfileResponse
struct ArticlesResponse: Codable {
var status: String
var totalResults: Int
var articles: [Article]
}
// MARK: - ProfileArticle
struct Article: Codable {
var source: Source
var author: String?
var title, articleDescription: String
var url: String
var urlToImage: String?
var publishedAt: String
var content: String?
enum CodingKeys: String, CodingKey {
case source, author, title
case articleDescription = "description"
case url, urlToImage, publishedAt, content
}
}
// MARK: - ProfileSource
struct Source: Codable {
var id: String?
var name: String
}
class MyViewController : UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white
let label = UILabel()
label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
label.text = "Hello World!"
label.textColor = .black
view.addSubview(label)
self.view = view
if let url = URL(string: "http://newsapi.org/v2/everything?q=bitcoin&from=2020-03-30&sortBy=publishedAt&apiKey=f0b7d2e59beb4bcdb7a737a25c3542bc") {
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data {
do {
let decoder = JSONDecoder()
let data = try decoder.decode(ArticlesResponse.self, from: data)
label.text = "\(data.status)"
} catch let error {
print(error)
}
}
}.resume()
}
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()