Arrays 带闭包数组的扩展
我有一个包含闭包的数组,其中包含下一种类型的闭包:Arrays 带闭包数组的扩展,arrays,swift,closures,extension-methods,Arrays,Swift,Closures,Extension Methods,我有一个包含闭包的数组,其中包含下一种类型的闭包: typealias FuncT = (()->Void) 我想将扩展方法添加到包含我的函数的数组中: extension Array where Element : FuncT { func execAll() { self.forEach { (f) in f() } } } 我发现编译错误: 类型“Element”约束为非协议、非类类型“FuncT” (又名“
typealias FuncT = (()->Void)
我想将扩展方法添加到包含我的函数的数组中:
extension Array where Element : FuncT {
func execAll() {
self.forEach { (f) in
f()
}
}
}
我发现编译错误:
类型“Element”约束为非协议、非类类型“FuncT”
(又名“()->()”)
如何将方法添加到包含函数的数组中 A约束
,其中A:B
将A
限制为类B
的子类,或符合协议B
的类型。函数类型是值类型,但不是类,并且不能符合协议
您需要的是一个“相同类型的需求”,其中a==B
。就你而言:
extension Array where Element == FuncT { ... }