Post

Replies

Boosts

Views

Activity

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
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
515
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
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
489
Sep ’21
Launch Screen Image
I have set in my info.plist file, the background and image I want for my launch screen. The image is 200px x 200px and for some reason, when the launch screen happens, the image gets stretched, not keeping the original size. How can I fix it ? Thank you
4
0
1.6k
Sep ’21
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
335
Sep ’21
Showing Decimal value
I am trying to present a decimal variable containing the value of 66.67. So I am trying to do: Text("\(myVariable, specifier: "%.2f")") //Instance method 'appendinterpolation(_:specifier) requires that Decimal conform to _formtSpecifiable Text("\(myVariable, specifier: "%.2f")" as String) //I receive extra argument specifier in call How can I fix it ? Thx
3
0
891
Sep ’21
DateFormatter - String to Date
struct MyView: View { var body: some View { //this value is coming from a JSON var myDate = "3000-01-01T08:00:00-08:00" //Here I'm calling my function formatDate, passing my myDate parameter: Text(formatDate(dateString: myDate)) } } func formatDate(dateString: String) -> Date { //first I dont know why my parameter gets here nil   let dateFormatter = ISO8601DateFormatter() //at the dateFormatter, doesnt matter the options I set, the return is always the same.   dateFormatter.formatOptions = [ .withFullDate,     .withFullTime,   ]   let finalDate = dateFormatter.date(from: dateString)!   return finalDate } I just need the get the date string, convert it to the format MM dd, yyyy - hh:mm How can I do it ? Thank you
2
0
665
Sep ’21
API Error
I have this API call in my application. For some reason, the response is coming back nil. If I try the right URL, a JSON will be returned and all the nodes filled out. So I'm trying to get the error inside the if let error = error { but even putting a breaking point in there, the code is never reached. What am I doing wrong ? Thanks func getProductById(productId: Int, completion: @escaping (ProductModel) -> ()) {     guard let url = URL(string: "https://mysite/api/products/82") 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       if let data = data {         print("data")      }      if let error = error {         print("error")      }       guard let data = data else { return }      do {         let product = try! JSONDecoder().decode(ProductModel.self, from: data)         DispatchQueue.main.async {           completion(product)         }       }      catch {         print(error)       }    }     .resume()
2
0
1.4k
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
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
Published Vars
I have an ObservableObject class that can return 2 different @published vars (product or products), depending on the function I call. But when I define the published vars, I am having an error message "Missing argument for parameter 'from' in call". It just happens for the first published var. What's the mistake? Thx class ProductStore: ObservableObject {   @Published var product = ProductModel() //getting error here   @Published var products: [ProductModel] = []   func getProducts() {     ProductApi().getProducts() { (products) in       self.products.append(contentsOf: products)     }   }   func getProductById(productId: Int) {     ProductApi().getProductById(productId: productId) { (product) in       self.product = product     }  } }
2
0
712
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
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
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
541
Aug ’21