# SwiftUI update variables in View

**URL:** <https://forums.kodeco.com/t/swiftui-update-variables-in-view/147143>\
**Category:** iOS\
**Created:** [August 30, 2021, 8:17pm UTC](https://forums.kodeco.com/t/swiftui-update-variables-in-view/147143 "2021-08-30T20:17:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattiav8](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/mattiav8/32/102075_2.png) [@mattiav8](https://forums.kodeco.com/u/mattiav8)\
**Post date:** [August 30, 2021, 8:17pm UTC](https://forums.kodeco.com/t/swiftui-update-variables-in-view/147143/1 "2021-08-30T20:17:16Z")

</div>

Hello everyone, I have this variable:

```auto
var total: [Double] {
        var amount: [Double] = []
        for i in 0..<price.count {
            let sum = quantity[i] * price[i]
            amount.append(sum)
        }
        return amount
    }

```

where its value depends on these two variables

```auto
@State var quantity: [Double] = [0.00]
@State var price: [Double] = [0.00]

```

The user should be able to change the quantity and price of an item in the view, and the variable should update itself automatically.  
Unfortunately it doesn’t work and I don’t know why.  
Could anyone help me find a solution?  
Thank you very much.

---

<div class="post-metadata">

**Author:** ![szymczyk](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/szymczyk/32/105896_2.png) [@szymczyk](https://forums.kodeco.com/u/szymczyk)\
**Post date:** [August 31, 2021, 9:59pm UTC](https://forums.kodeco.com/t/swiftui-update-variables-in-view/147143/2 "2021-08-31T21:59:54Z")

</div>

> Unfortunately it doesn’t work

How doesn’t it work? What do you expect to see happen? What is happening?

You haven’t provided enough information for anyone to help you. Update your question with the SwiftUI code where the person enters the quantity and price and the SwiftUI code that displays the total. You probably need to use the `.onChange` modifier to calculate the total when the quantity and price change.

One additional question. Why is your `total` variable an array? Shouldn’t it be a `Double` that contains the total price of everything the customer ordered?

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [February 13, 2022, 12:18pm UTC](https://forums.kodeco.com/t/swiftui-update-variables-in-view/147143/3 "2022-02-13T12:18:10Z")

</div>

This topic was automatically closed after 166 days. New replies are no longer allowed.
