以下代碼用於在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請幫我理解這是什麼意思?
*自包含*示例將會有所幫助。 –