問題描述
我實際上在做的事情更復雜,但歸根結底是能夠實現檢測某物是元組的功能,而不管其元素的類型是什么.
What I am actually doing is more complex but it comes down to being able to implement function to detect that something is a tuple, regardless of what the are the types of its elements.
這是我的方法不起作用(見最后一行的評論):
This is my approch that does not work (see comment on last line) :
func isTuple(b: Any) -> Bool {
return b is (Any, Any)
}
let myString = "aa"
let myDouble = 1.2
isTuple((myString, myDouble)) //returns false
為什么它不起作用?Any
不應該在元組中充當通配符"嗎?它是一個已知的 Swift 錯誤嗎(如果不是,我應該將其視為一個并報告)?還有其他方法可以使 isTupple
方法起作用嗎?
Why doesn't it work? Shouln't Any
act as a "wildcard" in tuples as well? Is it a known Swift bug (if not should I consider it one and report)? Is there other way how to make isTupple
method work?
@NateCook's answer 完全回答了原始問題,但這并不能幫助我做我想做的事情.就是這樣:
@NateCook's answer fully answers original question but does it does not help me to do what I am trying to do. This is it:
我不僅需要確定某物是元組,還需要在不知道這些值的確切類型的情況下將其分解為兩個值.
I need not only to determine that something is a tupple but also to break it into its 2 values without knowing the exact type of those values.
代碼如下:
func processIfTuple(b: Any) {
if reflect(b).disposition == MirrorDisposition.Tuple {
let (first, second) = b as (Any, Any) //error when casting
process(first)
process(second)
}
}
func process(value: Any) {
...
}
processIfTuple(("aa", 1.2))
這不起作用的原因與測試 b is (Any, Any)
不起作用的類似原因.這次嘗試投射時出現錯誤.這可以解決嗎?如果不是,是否應將其視為語言錯誤或缺少功能并報告?這肯定會導致大量代碼重復,因為我需要為一個元組測試所有可能的類型對.
This does not work from similar reason that the test b is (Any, Any)
does not work. This time there is error when trying to cast. Can this be solved? If not should it be considered language bug or missing feature and reported? It definitely leads to a LOT of code duplication because I need to test for all possible pairs of types for a tuple.
推薦答案
你可以使用 Swift 的嬰兒自省方法來解決這個問題:
You can use Swift's baby introspection methods to get at this:
func isTuple(b: Any) -> Bool {
return reflect(b).disposition == MirrorDisposition.Tuple
}
請注意,reflect
在很大程度上沒有記錄,可能只是作為對操場/調試器的支持,但據我所知,這是執行此操作的唯一方法.
Note that reflect
is largely undocumented and may only be there as support for the playground / debugger, but as far as I know this is the only way to do this.
要實現這一點,您需要深入了解 reflect()
為您提供的內容,這是一個符合 MirrorType
的結構,我稱之為反射,因為缺少一個更好的術語.您可以下標元組的反射以獲取元組成員的反射,然后將值返回為 Any
.此時,您可以使用可選綁定來安全地重新發現底層類型:
To achieve this you need to drill down into what reflect()
gives you, which is a struct that conforms to MirrorType
, which I call a reflection, for lack of a better term. You can subscript the reflection of a tuple to get reflections of the tuples members, and then get the value back out as Any
. At that point you can use optional binding to safely rediscover the underlying type:
func process(value: Any) {
println("Any (value)")
}
func process(value: String) {
println("String (value)")
}
func processTuple(b: Any) -> Bool {
let isTuple = reflect(b).disposition == MirrorDisposition.Tuple
let r = reflect(b)
for i in 0..<r.count {
println(r[i].0) // string holding tuple part name: ".0", ".1", etc
println(r[i].1.value) // the value of that tuple part: "aa", 1.2
process(r[i].1.value) // calls process(Any)
if let val = r[i].1.value as? String {
process(val) // calls process(String)
}
}
return isTuple
}
let myString = "aa"
let myDouble = 1.2
processTuple((myString, myDouble)) //returns false
輸出:
.0
aa
Any aa
String aa
.1
1.2
Any 1.2
這篇關于如何檢測該參數是兩種任意類型的元組?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!