Post

Replies

Boosts

Views

Activity

SF Symbols Fill
I am using SF symbols on my TabBar but even though I use the stroke version, it inserts the fill one. ProductsListView()         .tabItem {            Image(systemName: "book")            Text("Products")         } However, the image that shows up when I run the application in the book.fill Why this could be happening? Thx
2
0
2.3k
Nov ’21
Loading View on API call
I have a Loading view saying "Please wait" that I want to be presented until the data from the API comes back. However, self.showLoading = false is executed even before store.getHomeItems() is finished. How can I do it ? struct HomeView: View {   @State var showLoading = false ... if (self.showLoading == true) { LoadingView() } else { //my content } ... ScrollView { //my scroll content } .onAppear { self.showLoading = true       DispatchQueue.main.async {          store.getHomeItems()       } self.showloading = false ... } I also dont know if I can call Dispatch on my HomeView, since my API layer has also an another DispatchQueue. URLSession.shared.dataTask(with: url) { (data, _, _) in       guard let data = data else { return }               let myItems = try! JSONDecoder().decode([ItemModel].self, from: data)      DispatchQueue.main.asyncAfter(deadline: .now() + 10) {         completion(myItems)       }        }     .resume() Thank you
2
0
1.5k
Nov ’21
Open Modal
I have a ForEach listing a lot of eventos, having 1 image button for each of them. When the button is clicked, I will open a modal with the selectedEvento. The problem is even though I've the line "self.selectedEvento = evento" inside of the button actions, when I click the button for the first time, the selectedEvento is being passed as nil. However if I click a second button, the process will happen succesfully. Code: ... @State var showModal = false @State var selectedEvento: Evento! ... ForEach(store.eventos) { evento in           VStack() {             Button(action: {               self.selectedEvento = evento               self.showModal.toggle()                             }) {             WebImage(url: evento.thumbnail)             }           }           .sheet(isPresented: $showModal) {             if self.selectedEvento != nil {               //open detailView             } else {               Text("Some Error State goes here")             }           }         } ** Why is this happening ? Shoudn't the first click also to be passing the selectedEvento ? Why it doesnt happen ? Thx
1
0
324
Apr ’21
TabBar
In my SwiftUI app, I have a TabBar with 2 items: a) TabItem1 b) TabItem2 However on the View itself, I created 2 buttons: a) Button A b) Button B Now I need to switch TabBar items, using the buttons on the View. How can I do this ? ******* Button(action: { //call tabItem 1  }) {     Text("TabItem1") } Button(action: { //call tabItem 2  }) {     Text("TabItem2") } ***************************       TabView {       TabView1View()         .tabItem {           Text("TabItem1")         }               TabView2View()         .tabItem {           Text("TabItem2")         } Thank you
1
0
345
Apr ’21
Break Line
When I use: Text("My text \nhas a break line") The output is: My text has a break line PERFECT - I have "My text" and in the next line, I have "has a break line" However if I have: Text(mymodel.description) //where this is returning from an API call and mymodel.description contains "My text \nhas a break line") It doesn't work. The output is: My text \nhas a break line Why ?
1
0
1k
Jun ’21
Http Request with Querystring Parameter
I am trying to call an API, but I need to pass the parameter via querystring. But somehow it's not working. Where is the mistake ? func getProductById(productId: String, completion: @escaping (Product) -> ()) {     guard let url = URL(string: "https://mysite.com/product/" + productId) else { return }                       var request = URLRequest(url: url)     request.httpMethod = "GET"     request.setValue("application/json; charset=UTF-8", forHTTPHeaderField: "Content-Type")             URLSession.shared.dataTask(with: request) { (data, request, error) in       guard let data = data else { return }                       do {         let product = try! JSONDecoder().decode(Product.self, from: data)         DispatchQueue.main.async {           completion(product)         }       }               catch {         //print(error)       }             }     .resume()                }
1
0
1.7k
Aug ’21
EnvironmentObject
Checking an existing code, I could see an environmentObject set in my main file. What does it mean ? it means I am creating an instance of my store and making them available for the entire application ? Like a static instance in other languages ? or there is something else ? Thank you, @main struct myApp: App { var myStore = MyStore() var body: some Scene { WindowGroup { ContentView() .environmentObject(myStore)
1
0
415
Aug ’21
Multiple environmentObject
Trying to understand API calls in SwiftUI, I got some doubt about environmentObject.Here, I have a file Network.swift which contains a method to get Users. So I create an environmentObject on my ProjectNameApp.swift file (see code).But this is my question: Let's say I have an another file Products.swift containing methods related to the products API. How I would set it in my ProjectNameApp.swift file ? // Network.swift import SwiftUIclass Network: ObservableObject {     @Published var users: [User] = []    func getUsers() {         guard let url = URL(string: "https://jsonplaceholder.typicode.com/users") else { fatalError("Missing URL") }        let urlRequest = URLRequest(url: url)        let dataTask = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in             if let error = error {                 print("Request error: ", error)                 return             }            guard let response = response as? HTTPURLResponse else { return }            if response.statusCode == 200 {                 guard let data = data else { return }                 DispatchQueue.main.async {                     do {                         let decodedUsers = try JSONDecoder().decode([User].self, from: data)                         self.users = decodedUsers                     } catch let error {                         print("Error decoding: ", error)                     }                 }             }         }        dataTask.resume()     } } // ProjectNameApp.swift@main struct ProjectNameApp: App {     var network = Network()    var body: some Scene {         WindowGroup {             ContentView()                 .environmentObject(network)         }     } } Would be the right implementation something like this ? For each API group a new environmentObject ? // ProjectNameApp.swift@main struct ProjectNameApp: App {     var network = Network()     var product = Product()    var body: some Scene {         WindowGroup {             ContentView()                 .environmentObject(network)                 .environmentObject(product)         }     } } Thank you
1
0
3.3k
Aug ’21
Text - Formatted HTML
I have an app where I am bringing some formatted text from the database. The problem is when I use: Text(myDataComingFromJson), of course, my text will be something like: I've created an extension where I can remove the HTML tags but then all my text will be in only one paragraph and the lists won't exist () The second approach would use WKWebView but then I will have some headaches formatting text or loading extra views to do the job. Question: Is there a way to use Text directly, but keeping break lines and lists? Even though I have to change the content at the database level ? I also have tried using \r\n in the database but still didn't work. Is there a better solution? Thank you all
1
1
456
Aug ’21
Calling API
The main view on my app brings data from 3 different API calls. So when the app starts, the data are still being returned, so nothing is seen but just a blank space. They are basically a Thumbnail and its title. What would be the best solution for that: Have a temporary thumb and a text like "loading" OR any other approach? Thank you
1
0
723
Sep ’21
App Crashing
My app works fine but depending on something (that I still don't know), the app suddenly crashes. The only message error I have is: Thread 1: EXC_BAD_ACCESS (code=1, address=0x5c) How can I debug it and know exactly why this is happening ? Thank you,
1
0
357
Sep ’21
Instruments
My app crashes in specific parts and I am trying to identify why. I am using Instruments - Leaks but when I access this problematic part of the app, the app crashes and nothing is shown. What would be the best tool under Instruments umbrella to check these crashes? Thanks
1
0
443
Sep ’21
Init or body ?
I have a store, but need to call the method inside the body. However the only way it works is if I call it inside init otherwise I have an error. @ObservedObject var store = MyStore() init(category: categoryModel) {     store.getData(categoryId: category.id) //here works } var body: some View {         store.getData(categoryId: category.id) //Here I will have the error: Type '()' cannot conform to 'View' Why ? Thx
1
0
516
Nov ’21
Open View using Button
When I use TabView and select a TabItem, a chosen View will open right away. TabView {   HomeView()          .tabItem {             Image(systemName: "house")           Text("Home")           } } Now I am trying to do the same, using button. However with button, the chosen View will open under .sheet or .fullScreenCover. Button(text) { self.isPresented.toggle() } .sheet(isPresented: $isPresented) { self.content } How can I open the View using button, the same way it's opened using TabBar ? Thx
1
0
365
Sep ’21
Navigation Bar Color
I am trying to change the navigation bar background color of my view. Init() {    let coloredAppearance = UINavigationBarAppearance()     //coloredAppearance.configureWithTransparentBackground() //It doesnt matter what I use in here, it continues the same coloredAppearance.backgroundColor = UIColor(red: 202.0/255.0, green: 52.0/255.0, blue: 86.0/255.0, alpha: 1.0)     coloredAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]      UINavigationBar.appearance().standardAppearance = coloredAppearance UINavigationBar.appearance().compactAppearance = coloredAppearance UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance      UINavigationBar.appearance().tintColor = .white However if you get the red, green, blue, and alpha used above, you will see the color is being shown slightly different on the NavigationBar. What's the reason for that ? Thx
1
0
406
Nov ’21