to requires a temporary"错误-6ren"> to requires a temporary"错误-这是我的代码: var myTuple = ("bar", 42) func foo(_ bar: inout (arg1: String, arg2: Double)) { [...] } -6ren">
gpt4 book ai didi

swift - 将元组作为 inout 参数传递时出现 "implicit conversion from to requires a temporary"错误

转载 作者:可可西里 更新时间:2023-11-01 00:55:27 26 4
gpt4 key购买 nike

这是我的代码:

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)

Explanation by @Hamish:

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 - 将元组作为 inout 参数传递时出现 "implicit conversion from <tuple type> to <tuple type 2> requires a temporary"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49894807/

26 4 0