问题描述
如果我有一个签名(String,Bool)
的元组我不能把它转换为(String,Any)
。编译器说:
If I have a tuple with signature (String, Bool)
I cannot cast it to (String, Any)
. The compiler says:
但这应该可以工作,因为 Bool
可以安全地转换为任何
与为
。如果你这样做,几乎相同的错误抛出:
But this should work since Bool
can be casted safely to Any
with as
. Almost the same error gets thrown if you do something like that:
let any: Any = ("String", true)
any as! (String, Any) // error
any as! (String, Bool) // obviously succeeds
错误:
那么有什么解决方法,特别是对于第二种情况?因为你甚至不能将任何
转换到任何可以分别投射元素的任何元组(Any,Any)
p>
So is there any workaround especially for the second scenario? Because you cannot even cast Any
to any tuple (Any, Any)
where you could cast the elements separately.
推荐答案
元组不能被转换,即使它们包含的类型也可以。例如:
Tuples cannot be cast, even if the types they contain can. For example:
let nums = (1, 5, 9)
let doubleNums = nums as (Double, Double, Double) //fails
但是:
let nums : (Double, Double, Double) = (1, 5, 9) //succeeds
您的情况下的解决方法是转换单个元素,而不是元组本身:
The workaround in your case is to cast the individual element, not the Tuple itself:
let tuple = ("String", true)
let anyTuple = (tuple.0, tuple.1 as Any)
// anyTuple is (String, Any)
这是原因之一注释:
我认为这是一个实现限制,因为元组是复合类型像函数。同样,您不能创建元组的扩展(例如 extension(String,Bool){...}
)。
I think this is an implementation limitation because Tuples are compound types like functions. Similarly, you cannot create extensions of Tuples (e.g. extension (String, Bool) { … }
).
如果你实际上使用返回(String,Any)
的API,请尝试将其改为使用类或结构。但如果您无权改进API,您可以在第二个元素的类型上切换
:
If you're actually working with an API that returns (String, Any)
, try to change it to use a class or struct. But if you're powerless to improve the API, you can switch
on the second element's type:
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返回 Any
保证(String,Any)
,你运气不好。
If the API returns Any
and the tuple isn't guaranteed to be (String, Any)
, you're out of luck.
这篇关于元组“上转”在Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!