简体   繁体   中英

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.

someKindOfView
    .gesture(condition ? DragGesture() : nil)
 

Why is this possible?

Many thanks!!!!

Because the Gesture is generic protocol...

演示1

and so the gesture modifier accepts generic type conforming to Gesture

演示2

and there is such extension confirming Optional to Gesture

演示3

so in case of

.gesture(condition ? DragGesture() : nil)

compiler infers type as .gesture(_ gesture: DragGesture?, ... and all works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM