是否可以在Swift中将子类型的元组添加到类型的元组数组中?

时间:2018-10-20 18:54:18

标签: arrays swift inheritance types tuples

我想声明这个常量:

let simultaneousGestures: [(UIGestureRecognizer.Type, UIGestureRecognizer.Type)] = 
    [(UIPanGestureRecognizer, UIPinchGestureRecognizer), 
     (UIPanGestureRecognizer, UIRotationGestureRecognizer), 
     (UIPinchGestureRecognizer, UIRotationGestureRecognizer), 
     (UIPinchGestureRecognizer, UIPanGestureRecognizer),      
     (UIRotationGestureRecognizer, UIPanGestureRecognizer), 
     (UIRotationGestureRecognizer, UIPinchGestureRecognizer)
    ]

但是我得到这个错误:

Cannot convert value of type '(UIPanGestureRecognizer, UIPinchGestureRecognizer).Type' to expected element type '(UIGestureRecognizer.Type, UIGestureRecognizer.Type)'

我该如何实现?

1 个答案:

答案 0 :(得分:1)

你必须写

let simultaneousGestures: [(UIGestureRecognizer.Type, UIGestureRecognizer.Type)] =
    [(UIPanGestureRecognizer.self, UIPinchGestureRecognizer.self),
     (UIPanGestureRecognizer.self, UIRotationGestureRecognizer.self),
     (UIPinchGestureRecognizer.self, UIRotationGestureRecognizer.self),
     (UIPinchGestureRecognizer.self, UIPanGestureRecognizer.self),
     (UIRotationGestureRecognizer.self, UIPanGestureRecognizer.self),
     (UIRotationGestureRecognizer.self, UIPinchGestureRecognizer.self)
]