如何在 Swift 中关联类型的协议闭包中使用 Void 类型
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 }
}
现在我有一个 class 符合此协议,在闭包内具有 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 '()'?
并以这种 "weird" 语法结束:
var isCompleted: ( (()) -> Void )?
或这个冗长的:
typealias InputDataType = Void
var isCompleted: ( (InputDataType) -> Void )?
但是现在当我想在另一个 class 中分配这个闭包时,我需要在参数中明确使用“_”:
myClass.isCompleted = { _ in
// other code
}
不可能使用这样的东西:
myClass.isCompleted = {
// other code
}
那么你知道是否有可能得到像我引用和期望的例子那样更优雅的东西吗?
因为您已经通过协议声明 isCompleted
是必要的,所以您将不得不在 MyClass
中实现它,不幸的是使用了“奇怪”的语法。
但是,为了更好地使用它,您可以为 when 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
}
}
}
}
然后您可以方便地使用 class 上的方法,它为您提供了您正在寻找的最后一个代码示例:
var myClass = MyClass()
myClass.isCompleted = {
print("Hello World!")
}
myClass.isCompleted?()
我遇到了一个小问题,我找不到一种优雅的方法来使用具有关联类型的闭包作为协议中的 Void。
假设我有以下协议:
protocol MyProtocol {
associatedtype OutputDataType
associatedtype InputDataType
var isCompleted: ( (InputDataType) -> OutputDataType)? { get set }
}
现在我有一个 class 符合此协议,在闭包内具有 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 '()'?
并以这种 "weird" 语法结束:
var isCompleted: ( (()) -> Void )?
或这个冗长的:
typealias InputDataType = Void
var isCompleted: ( (InputDataType) -> Void )?
但是现在当我想在另一个 class 中分配这个闭包时,我需要在参数中明确使用“_”:
myClass.isCompleted = { _ in
// other code
}
不可能使用这样的东西:
myClass.isCompleted = {
// other code
}
那么你知道是否有可能得到像我引用和期望的例子那样更优雅的东西吗?
因为您已经通过协议声明 isCompleted
是必要的,所以您将不得不在 MyClass
中实现它,不幸的是使用了“奇怪”的语法。
但是,为了更好地使用它,您可以为 when 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
}
}
}
}
然后您可以方便地使用 class 上的方法,它为您提供了您正在寻找的最后一个代码示例:
var myClass = MyClass()
myClass.isCompleted = {
print("Hello World!")
}
myClass.isCompleted?()