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
Reply to Cannot find 'SecondView' in scope in swiftui error NavigationView.
Where is the definition of SecondView? When you show your code, you should better show whole code together. Whether your code works or not depends on other parts of your code.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Aug ’21
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:
Replies
Boosts
Views
Activity
Aug ’21
Reply to ScrollViewReader's scrollTo may be broken on iOS 15
I am not sure if this is sort of a bug or not, but you can send a bug report when you find some code once was working does not run in the latest beta.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Aug ’21
Reply to How to draw UIBezierPath with different colors?
CATransaction.begin() and CATransaction.commit() should be paired. Have you tried using 4 pairs of CATransaction.begin() and CATransaction.commit() for each stroke? By the way, Vision does not seem to be the right tag for your question. You are not using Vision framework, are you?
Topic: Machine Learning & AI SubTopic: General Tags:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
Aug ’21
Reply to SwiftUI Picker in a TextField with Core Data, error “Result of 'Picker <Label, SelectionValue, Content>' initializer is unused”
Editing feature is impractically limited in this site. You may need to use Your Answer to add some updates to your original post. And anyway, showing only partial code would not work well to get better responses sooner.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
Aug ’21
Reply to XCode for windows?
Your guess is right.
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
Aug ’21
Reply to Why Store
Depends on the use cases. Please explain more context about when you heard that you should create a Store.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
Aug ’21
Reply to Passing a (string) parameter to Web View addObserver? Swift 4
should I use a different mechanism? Maybe. Can you show your code?
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Aug ’21
Reply to SwiftUI Picker in a TextField with Core Data, error “Result of 'Picker <Label, SelectionValue, Content>' initializer is unused”
You cannot put view declarations inside closures passed to action methods like onTapGesture. What did you expect by putting a view (Picker) in onTapGesture?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Aug ’21
Reply to thread 8: fatal error
Can be caused by various reasons. Can you detailed messages and stack traces?
Replies
Boosts
Views
Activity
Aug ’21
Reply to xcode keeps showing this error that the view controller does not have identifier""
You need to see the Identity inspector and check if your HomeViewController has the right Storyboard ID:
Replies
Boosts
Views
Activity
Aug ’21