How to detect that parameter is a tuple of two arbitrary types?

前端 未结 2 853
鱼传尺愫
鱼传尺愫 2021-01-01 04:56

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 i

2条回答
  •  遥遥无期
    2021-01-01 05:02

    In Swift 3, it's now done this way:

    func isTuple(value: Any) -> Bool {
        if let type = Mirror(reflecting: value).displayStyle, type == .tuple {
            return true
        }
    
        return false
    }
    
    isTuple(value: ()) // true
    isTuple(value: (1, 2) // true
    isTuple(value: 3) // false
    

提交回复
热议问题