Post

Replies

Boosts

Views

Activity

Reply to Published Vars
Missing argument for parameter 'from' in call That happens when a proper initializer is not defined in ProductModel. If you cannot solve it by yourself, please show the definition of ProductModel.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’21
Reply to Identifiable ID
Both your JSON and code are not valid, you should better show the right things for readers not to be confused. Have you tried to make it a computed property? struct ProductModel: Identifiable, Codable, Hashable { var id: Int {productId} var productId: Int var name: String }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’21
Reply to Hi I am getting a blank screen when I run the following code in my Xcode project
Please use the Code Block feature of this site, when you show your code. Your code is sort of broken, it would confuse readers what you really tested. With fixing all of them by guess, the most critical issue is that you put onAppear at the wrong place. Please try this: 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 error = error { print("dataTask Error: \(error)") return } guard let data = data else { print("data is nil") return } //You should better not use `try?` when you do not know all the cases where errors would occur //Better use do-try-catch do { let decodedResult = try JSONDecoder().decode(Result.self, from: data) DispatchQueue.main.async { self.articles = decodedResult.articles } } catch { print("Decoding Error: \(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) //<- Move this line } .onAppear(perform: fetchData) //<- Here } } If onAppear is placed inside List, it would never be called while the list is empty. By the way, you should not make apiKey publicly open. I recommend you to disable it and re-get a new one.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’21
Reply to API Error
the response is coming back nil It is not clear how have you checked that. but even putting a breaking point in there, the code is never reached. API requests can fail with various reasons, in some cases, the error parameter is not nil. Please try this code and tell us what you get: (Please show your code well-formatted.) func getProductById(productId: Int, completion: @escaping (ProductModel) -> ()) { let url = URL(string: "https://mysite/api/products/82")! var request = URLRequest(url: url) request.httpMethod = "GET" //When you use `GET`, putting a `Content-Type` header does not make sense, usually... //request.setValue("application/json; charset=UTF-8", forHTTPHeaderField: "Content-Type") URLSession.shared.dataTask(with: request) { (data, response, error) in if let error = error { print("error: \(error)") return } guard let data = data else { print("data is nil") return } if let response = response { print("response: \(response)") } let responseText = String(data: data, encoding: .utf8) ?? "Unknown encoding" print("responseText: \(responseText)") do { //I recommend you never use `try!` let product = try JSONDecoder().decode(ProductModel.self, from: data) DispatchQueue.main.async { completion(product) } } catch { print(error) } } .resume() } By the way, you have not responded to the answers and comments properly. Good interaction will help you get better solutions sooner.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’21
Reply to EnvironmentObject
making them available for the entire application ? Not for the entire application, but for all the subviews where environmentObject is given. Like a static instance in other languages ? It's a more structured way than using a static instance, more like using Dependency Injection. But how you describe it would depend on what you have experienced, you should better try and use it yourself.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’21