以下代码用于在Swift 3.2中为我工作,但使用Swift 4的最新版本时,出现了一个我无法理解的奇怪错误。通用协议Swift 4错误
我试图创建一个通用的协议,像这样:
public protocol FactoryComponent {
associatedtype Argument
associatedtype Service
static var factory: (Resolver) -> (Argument) -> Service { get }
}
public extension FactoryComponent {
public typealias Factory = (Argument) -> Service
}
在这里使用它:
public extension Container {
@discardableResult
public func register<Component: FactoryComponent>(
factory componentType: Component.Type
) -> ServiceEntry<Component.Factory> { // On this line the error shows
return self.register(componentType.Factory.self) { resolver in
componentType.factory(resolver)
}
}
}
错误:
'组件' 没有一个成员类型命名为“工厂”;你的意思是'工厂'?
当然,自动修复没有帮助,因为错误是没用的......
我检查了斯威夫特4重大更改,并没有看到任何涉及通用协议。
可以somone请帮我理解这是什么意思?
*自包含*示例将会有所帮助。 –