[英]Error expected : Ternary conditional nil for not optional type of parameter in SwiftUI function
Code below works fine even if optional type isn't what the .gesture()
modifier really wants for its parameter type.即使可选类型不是.gesture()
修饰符真正想要的参数类型,下面的代码也可以正常工作。
someKindOfView
.gesture(condition ? DragGesture() : nil)
Why is this possible?为什么这可能?
Many thanks!!!!非常感谢!!!!
Because the Gesture
is generic protocol...因为Gesture
是通用协议...
and so the gesture
modifier accepts generic type conforming to Gesture
因此gesture
修饰符接受符合Gesture
的泛型类型
and there is such extension confirming Optional
to Gesture
并且有这样的扩展确认Gesture
Optional
so in case of所以如果
.gesture(condition ? DragGesture() : nil)
compiler infers type as .gesture(_ gesture: DragGesture?, ...
and all works.编译器将类型推断为.gesture(_ gesture: DragGesture?, ...
并且一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.