Post

Replies

Boosts

Views

Activity

Nil
I have a view which goes to my Store, gets data from an API and bringing back to the view. var productId = 123 @ObservedObject var productStore = ProductStore()       init() {     productStore.getById(productId: self.productid)   } The object from ProductStore is coming back but when I try to use it: Text(self.productStore.product!.title) I get: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value Why this is happening if the object doesnt have nil values ?
3
0
566
Aug ’21
Create Instance of class
When instanciating an array of ProductModel, I do this way and everything works fine: @Published var products = [ProductModel]() However If I just want one instance, I have an error: @Published var product = ProductModel() Missing argument for parameter 'from' in call Why is this problem happening ? Thx
2
0
516
Jun ’21
Why Store
When developing my code, I have seen people saying I should create a Store (eg: ProductStore) with ObservableObject/Published and inside of it, create functions to call the API (eg: ProductAPI) What's the reason for that ? Thank you
2
0
534
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
430
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.4k
Aug ’21
Identifiable ID
I have a JSON from my backend which returns: [ { productId : 1 name: productA }, { productid: 2 name: productB } } And on my SwiftUI project I have a model: struct ProductModel: Identifiable, Codable, Hashable { //var id = UUID() var productId = Int var name = String } In my View, when I do a foreach to present all the products coming from my JSON, I got a message saying my model needs to be Identifiable to be using the foreach. That was the reason I created the var id = UUID() on my model. Now I dont have the foreach error message, however, I get the error: Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "id", intValue: nil) Shouldn't it be ok since I am creating the ID on my model ? Thank you !
2
0
1.3k
Aug ’21
WebView
I am trying to parse some html inside my app. Not an entire html page, but just few blocks of html code. So I'm using Ink package for that. // FIRST I HAVE A WEBVIEW import SwiftUI import WebKitstruct WebView : UIViewRepresentable {   var html: String  func makeUIView(context: Context) -> WKWebView {     return WKWebView()   }  func updateUIView(_ webView: WKWebView, context: Context) {     webView.loadHTMLString(html, baseURL: nil)   }} // IN HERE I'M PARSING THE HTML AND ALSO ADDING SOME STYLING TO IT import Foundation import Ink class ParseContent: ObservableObject {   var markdown: String = ""   func parse() -> String {     let parser = MarkdownParser()     let html = parser.html(from: markdown)       let htmlStart = "<HTML><HEAD></HEAD><BODY style=\"padding: 140px; font-size: 120px; font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,Open Sans,Helvetica Neue,sans-serif\">"     let htmlEnd = "</BODY></HTML>"       return htmlStart + html + htmlEnd   } } // IN HERE I AM JUST USING MY PREVIOUSLY CREATED WEBVIEW WebView(html: myHtmlContent) .onAppear() {         htmlContent = ParseContent().parse()     } So here are my questions: Why the styling isn't working since I am concatenating the html ? I see it in a lot of tutorials but cant understand why not working. I would like to have the font inside my WebView, the same as I have for my other components like Text("abc") so I would keep a pattern and the visitor woudnt realize they are different components. How to set the WebView without a vertical scroll ? Keeping the entire height coming from the parsed html ? Thank you guys :)
2
0
1.1k
Jan ’23
App Crash - Message error
When I run my app with my phone connected, suddenly the app crashes. I don't have any information on the console, but I have the following message error in the @main line. Thread 1: EXC_BAD_ACCESS (code=1, address=0x28) @main struct MyApp: App { How can I know what is the problem and where to check for message errors when the app crashes ? Thank you
0
0
348
Sep ’21
Calling a View
I have a button in MyViewA and after clicking on the button search, I want to call MyViewB, passing the variable search. How can I call a different view after clicking a button ? var search: String = "abc" Button(action: { MyViewB(keyword: self.search) }) { Text("Search") } Thank you
2
0
512
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
376
Sep ’21
EXC_BAD_ACCESS
My app suddenly crashes exposing this message error: EXC_BAD_ACCESS (code=1, address=0x5c)  I tried to debug but can't find a solution. I believe using Instruments could be easier to find it. However Instruments has a lot of items. What would be the one responsible for debugging this kind of error I'm having ? Thank you
0
0
540
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
465
Sep ’21
NavigationLInk
I have an array with 10 different categories. So I create NavigationLink for each of them, calling MyListView and passing data about the category. NavigationView { List(categories) { category in NavigationLink(destination:           MyListView(category: category) )             {           CategoryRow(category: category)   } } } So when I click in one of these 10 categories, I will call MyListView. Inside MyListView I have a call to the API bringing a lot of information about the chosen category. The problem here is, even before clicking in any category, MyListView is being called and the API is called for all the 10 categories. I only want to call MyListView and the API inside it, after selecting the category. Whats the problem here ? Thx
3
0
654
Sep ’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
388
Sep ’21
TabBar and Views
I have a TabBar with 5 different views for each TabBar item. All these 5 views have long content wrapped in a ScrollView. So let's say I chose my 2nd TabBar item and scroll on this view. Then I move away to another view and when I hit the 2nd TabBar item again, I want this view scrolled to the top and not at the position I left on the first visualization. How can I do that? Thx
0
0
377
Nov ’21
Nil
I have a view which goes to my Store, gets data from an API and bringing back to the view. var productId = 123 @ObservedObject var productStore = ProductStore()       init() {     productStore.getById(productId: self.productid)   } The object from ProductStore is coming back but when I try to use it: Text(self.productStore.product!.title) I get: Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value Why this is happening if the object doesnt have nil values ?
Replies
3
Boosts
0
Views
566
Activity
Aug ’21
Create Instance of class
When instanciating an array of ProductModel, I do this way and everything works fine: @Published var products = [ProductModel]() However If I just want one instance, I have an error: @Published var product = ProductModel() Missing argument for parameter 'from' in call Why is this problem happening ? Thx
Replies
2
Boosts
0
Views
516
Activity
Jun ’21
Why Store
When developing my code, I have seen people saying I should create a Store (eg: ProductStore) with ObservableObject/Published and inside of it, create functions to call the API (eg: ProductAPI) What's the reason for that ? Thank you
Replies
2
Boosts
0
Views
534
Activity
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)
Replies
1
Boosts
0
Views
430
Activity
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
Replies
1
Boosts
0
Views
3.4k
Activity
Aug ’21
Identifiable ID
I have a JSON from my backend which returns: [ { productId : 1 name: productA }, { productid: 2 name: productB } } And on my SwiftUI project I have a model: struct ProductModel: Identifiable, Codable, Hashable { //var id = UUID() var productId = Int var name = String } In my View, when I do a foreach to present all the products coming from my JSON, I got a message saying my model needs to be Identifiable to be using the foreach. That was the reason I created the var id = UUID() on my model. Now I dont have the foreach error message, however, I get the error: Swift.DecodingError.keyNotFound(CodingKeys(stringValue: "id", intValue: nil) Shouldn't it be ok since I am creating the ID on my model ? Thank you !
Replies
2
Boosts
0
Views
1.3k
Activity
Aug ’21
WebView
I am trying to parse some html inside my app. Not an entire html page, but just few blocks of html code. So I'm using Ink package for that. // FIRST I HAVE A WEBVIEW import SwiftUI import WebKitstruct WebView : UIViewRepresentable {   var html: String  func makeUIView(context: Context) -> WKWebView {     return WKWebView()   }  func updateUIView(_ webView: WKWebView, context: Context) {     webView.loadHTMLString(html, baseURL: nil)   }} // IN HERE I'M PARSING THE HTML AND ALSO ADDING SOME STYLING TO IT import Foundation import Ink class ParseContent: ObservableObject {   var markdown: String = ""   func parse() -> String {     let parser = MarkdownParser()     let html = parser.html(from: markdown)       let htmlStart = "<HTML><HEAD></HEAD><BODY style=\"padding: 140px; font-size: 120px; font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,Open Sans,Helvetica Neue,sans-serif\">"     let htmlEnd = "</BODY></HTML>"       return htmlStart + html + htmlEnd   } } // IN HERE I AM JUST USING MY PREVIOUSLY CREATED WEBVIEW WebView(html: myHtmlContent) .onAppear() {         htmlContent = ParseContent().parse()     } So here are my questions: Why the styling isn't working since I am concatenating the html ? I see it in a lot of tutorials but cant understand why not working. I would like to have the font inside my WebView, the same as I have for my other components like Text("abc") so I would keep a pattern and the visitor woudnt realize they are different components. How to set the WebView without a vertical scroll ? Keeping the entire height coming from the parsed html ? Thank you guys :)
Replies
2
Boosts
0
Views
1.1k
Activity
Jan ’23
App Crash - Message error
When I run my app with my phone connected, suddenly the app crashes. I don't have any information on the console, but I have the following message error in the @main line. Thread 1: EXC_BAD_ACCESS (code=1, address=0x28) @main struct MyApp: App { How can I know what is the problem and where to check for message errors when the app crashes ? Thank you
Replies
0
Boosts
0
Views
348
Activity
Sep ’21
Calling a View
I have a button in MyViewA and after clicking on the button search, I want to call MyViewB, passing the variable search. How can I call a different view after clicking a button ? var search: String = "abc" Button(action: { MyViewB(keyword: self.search) }) { Text("Search") } Thank you
Replies
2
Boosts
0
Views
512
Activity
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,
Replies
1
Boosts
0
Views
376
Activity
Sep ’21
EXC_BAD_ACCESS
My app suddenly crashes exposing this message error: EXC_BAD_ACCESS (code=1, address=0x5c)  I tried to debug but can't find a solution. I believe using Instruments could be easier to find it. However Instruments has a lot of items. What would be the one responsible for debugging this kind of error I'm having ? Thank you
Replies
0
Boosts
0
Views
540
Activity
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
Replies
1
Boosts
0
Views
465
Activity
Sep ’21
NavigationLInk
I have an array with 10 different categories. So I create NavigationLink for each of them, calling MyListView and passing data about the category. NavigationView { List(categories) { category in NavigationLink(destination:           MyListView(category: category) )             {           CategoryRow(category: category)   } } } So when I click in one of these 10 categories, I will call MyListView. Inside MyListView I have a call to the API bringing a lot of information about the chosen category. The problem here is, even before clicking in any category, MyListView is being called and the API is called for all the 10 categories. I only want to call MyListView and the API inside it, after selecting the category. Whats the problem here ? Thx
Replies
3
Boosts
0
Views
654
Activity
Sep ’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
Replies
1
Boosts
0
Views
388
Activity
Sep ’21
TabBar and Views
I have a TabBar with 5 different views for each TabBar item. All these 5 views have long content wrapped in a ScrollView. So let's say I chose my 2nd TabBar item and scroll on this view. Then I move away to another view and when I hit the 2nd TabBar item again, I want this view scrolled to the top and not at the position I left on the first visualization. How can I do that? Thx
Replies
0
Boosts
0
Views
377
Activity
Nov ’21