无法调用"+ ="参数类型为(Int,@value Int)的参数列表

编程入门 行业动态 更新时间:2024-10-27 21:11:01
本文介绍了无法调用"+ ="参数类型为(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)的参数列表

本文发布于:2023-11-26 16:36:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634398.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   类型   列表   quot   Int

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!