Post

Replies

Boosts

Views

Activity

Reply to Limit width of wheel style picker on iOS
I do not know why this works, but an answer in an SO thread would work for you. Picker("", selection: Binding( get: { value }, set: { val in updateModel(val, pos) }) ) { ForEach(0 ..< 10, id: \.self) { i in Text("\(i)") } } .pickerStyle(.wheel) .labelsHidden() .frame(width: 20) .frame(idealHeight: height, maxHeight: .infinity) .clipped() .compositingGroup() //<-
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’21
Reply to Presentation single with code
Every time the user clicks the button the same window opens again and again. How to avoid that? It is not clear what you want to do. But I guess you just want to activate the window if it is already opened. Then, you should not instantiate a new instance at each time: private var controller: NSWindowController? = { let storyboard:NSStoryboard = NSStoryboard(name: "Main", bundle: nil) return storyboard.instantiateController(withIdentifier: "finestra3") as? NSWindowController }() @IBAction func obrirAmbCodi(_ sender: NSButton) { controller?.showWindow(self) }
Topic: Programming Languages SubTopic: Swift 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 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 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 Can i turn a touchesCancelled onto touchesEnded?
You can create another method and call it both from touchesCancelled and touchesEnded.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Aug ’21
Reply to Inivation code
It is not possible in this site.
Replies
Boosts
Views
Activity
Aug ’21
Reply to Limit width of wheel style picker on iOS
I do not know why this works, but an answer in an SO thread would work for you. Picker("", selection: Binding( get: { value }, set: { val in updateModel(val, pos) }) ) { ForEach(0 ..< 10, id: \.self) { i in Text("\(i)") } } .pickerStyle(.wheel) .labelsHidden() .frame(width: 20) .frame(idealHeight: height, maxHeight: .infinity) .clipped() .compositingGroup() //<-
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Aug ’21
Reply to Adding Restores Everything
Having two separate properties favArr and noRepFav may be causing the issue.
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Aug ’21
Reply to Presentation single with code
Every time the user clicks the button the same window opens again and again. How to avoid that? It is not clear what you want to do. But I guess you just want to activate the window if it is already opened. Then, you should not instantiate a new instance at each time: private var controller: NSWindowController? = { let storyboard:NSStoryboard = NSStoryboard(name: "Main", bundle: nil) return storyboard.instantiateController(withIdentifier: "finestra3") as? NSWindowController }() @IBAction func obrirAmbCodi(_ sender: NSButton) { controller?.showWindow(self) }
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Aug ’21
Reply to Soup Chef Sample Code Project Won't Open in Xcode 12
The latest sample code page has this description: Soup Chef: Accelerating App Interactions with Shortcuts Availability iOS 14.3+  Xcode 13.0+ Beta ... Seems you need to download the latest beta version of Xcode 13.
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
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 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 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 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 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 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 XCode for windows?
Your guess is right.
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