Cannot call "+ =" with argument list of type (Int, @value Int)

I have a class Transaction

that is of type var amountInt

. I want to access it from another class where I have array of Transactions

and sum all their sums.

So I have this piece of code

func computeTotal()-> Int{
    let total = 0
    for transaction in transactions{
        //get the amounts of each and sum all of them up
        total += transaction.amount
    }
    return total
}

      

But it gives me an error

Cannot call "+ =" with argument list of type (Int, @value Int)

What could be causing this? I know that in Swift both operands must be of the same type, but they are both Int types in my code.

+3


source to share


1 answer


let

creates an immutable value. You need to use var

for example:



func computeTotal()-> Int{
    var total = 0
    for transaction in transactions{
        //get the amounts of each and sum all of them up
        total += transaction.amount
    }
    return total
}

      

+3


source







All Articles