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
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
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
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
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
I have a form and at the end of it, a button.
Form {
Section {
} //close section
button
} //close form
However, the button is appearing surrounded by white space, like inside a textbox.
What's the reason for that?
I have a TabBar with 8 items.
And as everybody knows, more than 5 makes the 5th item the MORE button and it's perfect.
The problem is when I hit it, the button just blinks.
If I hit again, then the other options are shown.
Why ?
Thx
In my SwiftUI project, I am trying to convert HTML to Text.
Text("h1Test/h1p Test/pbr/spanTest/spanulliitem1/liliitem2/li/ul")
.font(.body)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.bottom, 16)
Is there a way to make it happen ?
Thank you
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
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()
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
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 !
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
} }
}
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 :)
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
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