Hi
I want to add new data to a list in my project
but I can't do it
I have a ContentView.swift view for showing products list
and in another view (ShopView) I want to add data to products array
My products array and my addProduct() function
in the Products.swift file
Please help me
Thanks
ContentView.swift
struct ContentView: View {
@ObservedObject var cart = Products()
var body: some View {
NavigationView{
List {
ForEach(cart.products) { product in
Text("\(product.name) \(product.price)$")
}
}
.navigationBarItems(
trailing: NavigationLink(destination: Shop()) {
Text("Go Shop")
})
.navigationBarTitle("Cart")
}
}
}
Product.swift
struct Product: Identifiable {
var id = UUID()
var name: String
var price: Int
}
Products.swift
struct Shop: View {
@ObservedObject var cart = Products()
var body: some View {
VStack{
Button("Add Product To Cart") {
cart.addProduct(product: Product(name: "Name", price: 399))
}
}
}
}
Products.swift
class Products: ObservableObject {
@Published var products = [Product]()
func addProduct(product: Product) {
products.append(product)
print("Product Added")
}
}
Post
Replies
Boosts
Views
Activity
I want an alert to be displayed when the user is moved to a new page without any action and quickly.
I use NavigationLink
I try for this
@State private var showAlert = true
but there was no result
you can help me ?
i already used .padding(.top, UIApplication.shared.windows[0].safeAreaInsets.top)
but in swiftui 3 and iOS 15 i got this warning
'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead
so in new version what is the best way for getting safe area insets?
i can't use GeometryRedaer
I just downloaded Xcode 15 beta from Apple Developer website and I chose iOS 17 SDK/Simulator to download too. But now after creating a project and running it on a simulator my Xcode doesn't detect iOS 17 SDK. I also moved the "simruntime" file to Library/Developer/CoreSimulator/Profiles folder.