无法调用"+ ="参数类型为(Int,@value Int)的参数列表 [英] Cannot invoke "+=" with an argument list of type (Int, @value Int)
本文介绍了无法调用"+ ="参数类型为(Int,@value Int)的参数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类Transaction
,其类型为Int
的可变金额.
我想从另一个有array of Transactions
的类中访问它,并对它们的全部金额求和.
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.
所以我有这段代码
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
}
但这给我一个错误
无法使用类型为(Int,@value Int)的参数列表调用"+ ="
是什么原因引起的?我知道在Swift中,两个操作数必须是同一类型,但是在我的代码中它们都是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.
推荐答案
let
创建不可变的值.您需要使用var
,例如:
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
}
这篇关于无法调用"+ ="参数类型为(Int,@value Int)的参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文