简体   繁体   English

预期错误:SwiftUI 函数中非可选参数类型的三元条件 nil

[英]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是通用协议...

演示1

and so the gesture modifier accepts generic type conforming to Gesture因此gesture修饰符接受符合Gesture的泛型类型

演示2

and there is such extension confirming Optional to Gesture并且有这样的扩展确认Gesture Optional

演示3

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.

相关问题 将nil传递给具有通用类型约束的可选参数的编译错误 - Compile error for passing nil to an optional parameter with a generic type constraint 三元条件运算符为零/不为零 - Ternary Conditional Operator for nil/not nil 错误Nil与预期的参数类型'() - > Void'不兼容 - error Nil is not compatible with expected argument type '() -> Void' 在协议扩展中将nil传递给具有可选的一般约束参数的函数 - Passing nil to a function with an optional generically constrained parameter in a protocol extension SwiftUI DatePicker Binding optional Date, valid nil - SwiftUI DatePicker Binding optional Date, valid nil Swift中的可选类型错误:致命错误:解开可选值时意外发现nil - Optional type error in Swift: fatal error: unexpectedly found nil while unwrapping an Optional value Swift错误:条件绑定的初始化程序必须具有可选类型,而不是'()' - Swift error: Initializer for conditional binding must have Optional type, not '()' 条件绑定中的Xcode 6.1错误绑定值必须是Optional类型 - Xcode 6.1 Error bound value in a conditional binding must be Optional type 条件绑定的初始化程序必须具有可选类型,而不是'Error.Protocol' - Initializer for conditional binding must have Optional type, not 'Error.Protocol' 出现此错误:条件绑定的初始化程序必须具有Optional类型,而不是'Bool' - Getting this error : Initializer for conditional binding must have Optional type, not 'Bool'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM