Swift 中的元组 "upcasting"

Tuple "upcasting" in Swift

如果我有一个带有签名 (String, Bool) 的元组,我不能将它转换为 (String, Any)。编译器说:

error: cannot express tuple conversion '(String, Bool)' to '(String, Any)'

但这应该可以工作,因为可以使用 asBool 安全地转换为 Any。如果你这样做,几乎会抛出同样的错误:

let any: Any = ("String", true)
any as! (String, Any) // error
any as! (String, Bool) // obviously succeeds

错误:

Could not cast value of type '(Swift.String, Swift.Bool)' to '(protocol<>, protocol<>)'

那么对于第二种情况有什么解决方法吗?因为您甚至不能将 Any 转换为任何元组 (Any, Any),您可以在其中单独转换元素。

Swift 编译器将强制要求您明确指定类型。因此,如果您将其声明为 (String, Bool),它将不允许转换。

以下在 Playground 中按预期工作:

var specificTuple : (String, Bool) = ("Hi", false)
var generalTuple  : (Any, Any)     = ("Hi", false)

var gany = generalTuple
gany.1 = "there"
gany                           // (.0 "Hi", .1 "there")

var spany = specificTuple
spany.1 = "there"             // error

您可以临时创建 (Any, Any) 元组,但您需要将其分解

var any : (Any, Any)  = (specificTuple.0, specificTuple.1)
any.1 = "there"
any                          // (.0 "Hi", .1 "there")

不能强制转换元组,即使它们包含的类型可以。例如:

let nums = (1, 5, 9)
let doubleNums = nums as (Double, Double, Double) //fails

但是:

let nums : (Double, Double, Double) = (1, 5, 9) //succeeds

您的解决方法是转换单个元素,而不是元组本身:

let tuple = ("String", true)
let anyTuple = (tuple.0, tuple.1 as Any)
// anyTuple is (String, Any)

这是原因之一the Swift documentation注:

Tuples are useful for temporary groups of related values. They are not suited to the creation of complex data structures. If your data structure is likely to persist beyond a temporary scope, model it as a class or structure, rather than as a tuple.

我认为这是一个实现限制,因为元组是 复合类型,类似于函数。同样,您不能创建元组的扩展(例如 extension (String, Bool) { … })。


如果您实际上正在使用 returns (String, Any) 的 API,请尝试将其更改为使用 class 或结构。但是如果你无力改进 API,你可以 switch 第二个元素的类型:

let tuple : (String, Any) = ("string", true)

switch tuple.1 {

case let x as Bool:
    print("It's a Bool")
    let boolTuple = (tuple.0, tuple.1 as! Bool)

case let x as Double:
    print("It's a Double")
    let doubleTuple = (tuple.0, tuple.1 as! Double)

case let x as NSDateFormatter:
    print("It's an NSDateFormatter")
    let dateFormatterTuple = (tuple.0, tuple.1 as! NSDateFormatter)

default:
    print("Unsupported type")
}

如果 API returns Any 并且元组不能保证是 (String, Any),那你就不走运了。