久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何檢測該參數是兩種任意類型的元組?

How to detect that parameter is a tuple of two arbitrary types?(如何檢測該參數是兩種任意類型的元組?)
本文介紹了如何檢測該參數是兩種任意類型的元組?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我實際上在做的事情更復雜,但歸根結底是能夠實現檢測某物是元組的功能,而不管其元素的類型是什么.

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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

how to set scrollview content size in swift 3.0(如何在 swift 3.0 中設置滾動視圖內容大小)
Create partial-screen UIPageViewController programmatically(以編程方式創建部分屏幕 UIPageViewController)
UIImageView zoom and pinch in UIScrollView(UIImageView 在 UIScrollView 中縮放和捏合)
Make background color change during scroll(在滾動期間更改背景顏色)
How to set UIScrollView Height in Swift(如何在 Swift 中設置 UIScrollView 高度)
Disable UIPageViewController bouncing - Swift(禁用 UIPageViewController 彈跳 - Swift)
主站蜘蛛池模板: 国产一级一片免费播放 | 99久久婷婷国产综合精品电影 | 日韩精品一区二区三区在线观看 | 欧美另类视频在线 | 久久中文视频 | 精品在线一区 | 国产一区91精品张津瑜 | 日韩av在线一区二区 | 国产99视频精品免费视频7 | 色必久久 | 在线一区二区三区 | 亚洲欧美日韩一区二区 | 亚洲天堂av一区 | 99久久精品国产一区二区三区 | 美女张开腿露出尿口 | 欧美精品一区二区三 | 国产精品久久性 | 久久这里只有精品首页 | 欧美日韩视频 | 国产美女视频一区 | 日本久久网 | 成人a网| 亚洲一av | 午夜精品久久久 | 亚洲欧洲一区 | 欧美在线观看黄色 | 国产一区二区在线免费 | 黄视频网址 | 亚洲午夜三级 | 久久久久久一区 | 欧美精品一区二区三区在线 | 亚洲精品久久久久久久久久久 | 成人在线精品视频 | 在线电影日韩 | 欧美成人免费在线视频 | 亚洲自拍偷拍av | 欧美日韩免费一区二区三区 | 99热热热| av一级久久| 伊人久久在线 | 久久国产婷婷国产香蕉 |