[英]how to use Void type in a closure of protocol with associatedtype in Swift
我有一个小问题,我找不到一种优雅的方法来使用具有关联类型的闭包作为协议中的 Void。
假设我有以下协议:
protocol MyProtocol {
associatedtype OutputDataType
associatedtype InputDataType
var isCompleted: ( (InputDataType) -> OutputDataType)? { get set }
}
现在我有一个符合这个协议的类,在闭包内有 Void 类型,如下所示:
class MyClass: MyProtocol {
var isCompleted: ( () -> Void )? // Error: Type 'MyClass' does not conform to protocol 'MyProtocol'
}
所以我试过这个:
var isCompleted: ( (Void) -> Void )? // warning: When calling this function in Swift 4 or later, you must pass a '()' tuple; did you mean for the input type to be '()'?
并以这种“奇怪”的语法结束:
var isCompleted: ( (()) -> Void )?
或者这个冗长的:
typealias InputDataType = Void
var isCompleted: ( (InputDataType) -> Void )?
但是现在当我想在另一个类中分配这个闭包时,我需要在参数中明确使用“_”:
myClass.isCompleted = { _ in
// other code
}
不可能使用这样的东西:
myClass.isCompleted = {
// other code
}
那么你知道是否有可能获得更优雅的东西,比如我引用和预期的例子?
因为您已经通过协议根据需要声明了isCompleted
,所以您必须在MyClass
实现它,不幸的是使用了“奇怪”的语法。
但是,为了更好地使用它,您可以在InputDataType == Void
时为协议添加一个扩展,这样可以方便地访问isCompleted
而无需显式传递Void
......像这样的事情:
extension MyProtocol where InputDataType == Void {
var isCompleted: (() -> OutputDataType)? {
get {
guard let isCompleted = self.isCompleted else { return nil }
return { return isCompleted(()) }
}
set {
if let newValue = newValue {
self.isCompleted = { _ in return newValue() }
} else {
self.isCompleted = nil
}
}
}
}
然后您可以通过方便的方式在您的类上使用该方法,这为您提供了您正在寻找的最后一个代码示例:
var myClass = MyClass()
myClass.isCompleted = {
print("Hello World!")
}
myClass.isCompleted?()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.