swift - "implicit conversion from <tuple type> to <tuple type 2> requires a temporary"将元组作为 inout 参数传递时出错

标签 swift tuples inout

这是我的代码:

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

相关文章:

python - 如何从元组列表中获取值并创建一个新列表

python - 根据列表中成员的可用性过滤元组列表

swift - 如何保证 inout 参数不会改变 Type 并且不会在函数中变成 nil

Swift 将协议(protocol)类型转换为 struct 并作为 inout 传递

swift - 在实例函数中初始化实例变量

ios - 当我缩放我的 mapView 以显示我的注释时,所选注释部分不在屏幕上

swift - 检查 Swift 中常量的值

C++0x : iterating through a tuple with a function

ios - SwiftUI/在圈内添加照片

swift - 在 Swift 中动态调整一个静态表格 View 单元格的高度