这是我的代码:
var myTuple = ("bar", 42)
func foo(_ bar: inout (arg1: String, arg2: Double)) {
[...]
}
foo(&myTuple)
此行出现以下错误:
foo(&myTuple)
Cannot pass immutable value as inout argument: implicit conversion from '(String, Double)' to '(arg1: String, arg2: Double)' requires a temporary
最佳答案
实际问题是您的元组变量缺少函数中存在的标签。将其替换为以下内容:
var myTuple = (arg1: "bar", arg2: 42)
The problem is that an implicit conversion is required for a
(String, Int)
to match up with a(arg1: String, arg2: Int)
– by performing the implicit coercion, the compiler ends up with a temporary rvalue which cannot then be passed inout. That's why the error (somewhat confusingly) talks about an immutable value.
关于swift - "implicit conversion from <tuple type> to <tuple type 2> requires a temporary"将元组作为 inout 参数传递时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49894807/