Swift 中关联类型符合协议的异构数组
Heterogeneous array that conforms to protocol with associated type in Swift
我有一个带有关联类型 AType
和函数 aFunc
的协议 AProtocol
。我想通过使用其元素 aFunc
函数的结果来扩展 Array
以使其符合协议。显然,这只有在数组元素符合 Aprotocol
并且具有相同的关联类型时才有可能,所以我设置了这个玩具示例:
protocol AProtocol {
associatedtype AType
func aFunc(parameter:AType) -> Bool
}
extension Array : AProtocol where Element : AProtocol, Element.AType == Int {
func aFunc(parameter: Int) -> Bool {
return self.reduce(true, { r,e in r || e.aFunc(parameter: parameter) })
}
}
extension String : AProtocol {
func aFunc(parameter: Int) -> Bool {
return true
}
}
extension Int : AProtocol {
func aFunc(parameter: Int) -> Bool {
return false
}
}
这适用于只包含一种类型的数组:
let array1 = [1,2,4]
array1.aFunc(parameter: 3)
但是对于异构数组,我得到错误 Heterogeneous collection literal could only be inferred to '[Any]'; add explicit type annotation if this is intentional
然后 Value of type '[Any]' has no member 'aFunc'
如果注释如下:
let array2 = [1,2,"Hi"] as [Any]
array2.aFunc(parameter: 3)
是否可以按我的意愿扩展 Array,允许异构数组,只要它们符合 AProtocol
并具有相同的 AType
?
看看这是否符合您的需求。
方法:
删除关联类型
实施:
protocol BProtocol {
func aFunc(parameter: BProtocol) -> Bool
}
extension String : BProtocol {
func aFunc(parameter: BProtocol) -> Bool {
return true
}
}
extension Int : BProtocol {
func aFunc(parameter: BProtocol) -> Bool {
return false
}
}
extension Array : BProtocol where Element == BProtocol {
func aFunc(parameter: BProtocol) -> Bool {
return self.reduce(true, { r,e in r || e.aFunc(parameter: parameter) })
}
}
正在调用:
let a1 : [BProtocol] = [1, 2, 3, "Hi"]
let boolean = a1.aFunc(parameter: 1)
我有一个带有关联类型 AType
和函数 aFunc
的协议 AProtocol
。我想通过使用其元素 aFunc
函数的结果来扩展 Array
以使其符合协议。显然,这只有在数组元素符合 Aprotocol
并且具有相同的关联类型时才有可能,所以我设置了这个玩具示例:
protocol AProtocol {
associatedtype AType
func aFunc(parameter:AType) -> Bool
}
extension Array : AProtocol where Element : AProtocol, Element.AType == Int {
func aFunc(parameter: Int) -> Bool {
return self.reduce(true, { r,e in r || e.aFunc(parameter: parameter) })
}
}
extension String : AProtocol {
func aFunc(parameter: Int) -> Bool {
return true
}
}
extension Int : AProtocol {
func aFunc(parameter: Int) -> Bool {
return false
}
}
这适用于只包含一种类型的数组:
let array1 = [1,2,4]
array1.aFunc(parameter: 3)
但是对于异构数组,我得到错误 Heterogeneous collection literal could only be inferred to '[Any]'; add explicit type annotation if this is intentional
然后 Value of type '[Any]' has no member 'aFunc'
如果注释如下:
let array2 = [1,2,"Hi"] as [Any]
array2.aFunc(parameter: 3)
是否可以按我的意愿扩展 Array,允许异构数组,只要它们符合 AProtocol
并具有相同的 AType
?
看看这是否符合您的需求。
方法:
删除关联类型
实施:
protocol BProtocol {
func aFunc(parameter: BProtocol) -> Bool
}
extension String : BProtocol {
func aFunc(parameter: BProtocol) -> Bool {
return true
}
}
extension Int : BProtocol {
func aFunc(parameter: BProtocol) -> Bool {
return false
}
}
extension Array : BProtocol where Element == BProtocol {
func aFunc(parameter: BProtocol) -> Bool {
return self.reduce(true, { r,e in r || e.aFunc(parameter: parameter) })
}
}
正在调用:
let a1 : [BProtocol] = [1, 2, 3, "Hi"]
let boolean = a1.aFunc(parameter: 1)