Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a class Transaction which has a var amount of type Int. I want to access it from another class, where I have an array of Transactions and sum all of their amounts.

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 invoke "+=" with an argument list of type (Int, @value Int)

What can cause that? I know that in Swift both operands must be the same type, but they are both of type Int in my code.

like image 271
Romaldowoho Avatar asked Dec 02 '25 14:12

Romaldowoho


1 Answers

let creates an immutable value. You need to use var, like:

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
}
like image 175
John Gibb Avatar answered Dec 04 '25 06:12

John Gibb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!