[ad_1]
My SwiftUI utility features as follows I add customized merchandise in content material view with a inventory title & depreciated worth & I add inventory names and ranges in StockInput view. My downside happens within the pay perform that it would not minus the depreciated worth from the inventory quantity if the inventory names are an identical.. Right here is snippets of my code (No errors happen its only a case the place nothing occurs.
Contentview the place I add customized merchandise
TextField("Identify", textual content: $title)
TextField("Value", textual content: $value)
TextField("Inventory Identify", textual content: $stockName)
TextField("Depreciated Quantity", textual content: $depreciatedAmount)
Button(motion: {
self.showingImagePicker = true
}) {
Picture(systemName: "picture")
}
}
.padding()
Button(motion: {
if let value = Double(self.value), let depreciatedAmount = Int(self.depreciatedAmount) {
let product = ProductCustom(title: self.title, value: value, imageUrl: self.imageUrl, stockName: self.stockName, depreciatedAmount: depreciatedAmount)
self.productData.customproducts.append(product)
}
}) {
Textual content("Add Product")
.font(.headline)
.foregroundColor(.white)
.padding()
.background(Colour.orange)
.cornerRadius(30)
}``
StockView the place I add inventory
Textual content("Add Present Inventory & Quantity")
.font(.largeTitle)
.foregroundColor(.orange)
.padding()
HStack {
ForEach(stocksData.shares, id: .title) { inventory in
ZStack {
RoundedRectangle(cornerRadius: 10)
.fill(Colour.orange)
.shadow(radius: 5)
VStack(alignment: .main) {
Textual content("(inventory.title)")
.font(.headline)
Textual content("(inventory.quantity) (inventory.unit)")
.font(.subheadline)
}.padding()
}
.body(width: 100, top: 100)
}
}
.padding()
TextField("Inventory title", textual content: $stockName)
.padding()
.textFieldStyle(RoundedBorderTextFieldStyle())
.cornerRadius(5.0)
TextField("Inventory quantity", worth: $stockAmount, formatter: NumberFormatter())
.padding()
.textFieldStyle(RoundedBorderTextFieldStyle())
.cornerRadius(5.0)
Picker(choice: $unitSelection, label: Textual content("Unit")) {
ForEach(models.indices) {
Textual content(self.models[$0])
}
}
.pickerStyle(SegmentedPickerStyle())
Button(motion: {
self.stocksData.shares.append((title: self.stockName, quantity: self.stockAmount, unit: self.models[self.unitSelection]))
}, label: {
Textual content("Add Inventory")
})`
Detailed Statistics the place I show inventory ranges
` HStack {
ForEach(stocksData.shares, id: .title) { inventory in
VStack {
Textual content("(inventory.title)")
ZStack {
Circle()
.stroke(Colour.grey, lineWidth: 20)
Circle()
.trim(from: 0, to: CGFloat(inventory.quantity) / CGFloat(inventory.quantity))
.stroke(Colour.orange, lineWidth: 20)
.rotationEffect(Angle(levels: -90))
Textual content("(inventory.quantity)")
.font(.largeTitle)
}
.body(width: 100, top: 200)
}
.padding()
}
}`
`
And at last my pay perform
func pay() {
for product in customproducts {
if let productStockName = product.stockName, let index = stocksData.shares.firstIndex(the place: { $0.title == productStockName }) {
var inventory = stocksData.shares[index]
if let depreciatedAmount = product.depreciatedAmount {
inventory.quantity -= depreciatedAmount
stocksData.shares[index] = inventory
}
}
}
I anticipate the inventory stage to lower, I’ve tried many alternative variations of the pay perform with no success.. the inventory stage stays the identical even tho I’ve matching inventory names in content material view and inventory enter view
[ad_2]