Post

Replies

Boosts

Views

Activity

Hi I am getting a blank screen when I run the following code in my Xcode project
// //  ContentView.swift //  NewsReader // //  Created by Rashid  Vohra on 7/7/21. // import SwiftUI struct Result: Codable {     var articles: [Article] } struct Article: Codable {     var url: String     var title: String     var description: String?     var urlToImage: String? } struct ContentView: View {     private let url = "https://newsapi.org/v2/top-headlines?country=us&category=business&apiKey=49d5bfa113c34ec0af781fab38395996"               @State private var articles = [ Article ()          func fetchData() {         guard let url = URL(string: url) else {             print("URL is not valid")             return         }                  let request = URLRequest(url: url)                  URLSession.shared.dataTask(with: request) {             data, response, error in             if let data = data {                 if let decodedResult = try?                     JSONDecoder().decode(                         Result.self, from: data) {                                          DispatchQueue.main.async {                                                self.articles =                             decodedResult.articles                     }                     return                 }             }             print ("Error: (error?.localizedDescription ?? "Unknown Error") ")         }.resume()     }          var body: some View {                           List(articles, id: .url) { item in             HStack(alignment: .top) {                                 VStack(alignment: .leading) {                     Text(item.title)                         .font(.headline)                     Text(item.description ?? "")                         .font(.footnote)                 }             }.onAppear(perform: fetchData)         }     } } struct ContentView_Previews: PreviewProvider {     static var previews: some View {         ContentView()     } }
2
0
1k
Aug ’21