I am encountering a weird bug lately, while trying to conform a class to a given protocol the compiler rejects the typealias
declaration.
My code looks like this:
/// Protocol from CareKit
public protocol OCKTaskViewSynchronizerProtocol: OCKAnyTaskViewSynchronizerProtocol {
associatedtype View: UIView & OCKTaskDisplayable
func makeView() -> View
func updateView(_ view: View, context: OCKSynchronizationContext<OCKTaskEvents?>)
}
/// My own protocol
protocol ConditionalTaskViewProtocol: UIView, OCKTaskDisplayable, OCKEventUpdatable {
init()
}
/// class that should conform to `OCKTaskViewSynchronizerProtocol`
/// Error gets thrown here:
/// Type 'ConditionalTaskViewSynchronizer<First, Second>' does not conform to protocol 'OCKTaskViewSynchronizerProtocol'"
class ConditionalTaskViewSynchronizer<First: ConditionalTaskViewProtocol, Second: ConditionalTaskViewProtocol>: OCKTaskViewSynchronizerProtocol {
typealias View = ConditionalTaskViewProtocol
let condition: () -> Bool
public init(condition: @escaping () -> Bool) {
self.condition = condition
}
func makeView() -> View {
if condition() {
return First()
} else {
return Second()
}
}
func updateView(_ view: View, context: OCKSynchronizationContext<OCKTaskEvents?>) {
view.updateWith(event: context.viewModel?.firstEvent, animated: context.animated)
}
}
The fix that the compiler suggests is to conform the type of the typealias View
to UIView
. But I already conformed ConditionalTaskViewProtocol
to both UIView
and OCKTaskDisplayable
.
Am I missing something here?
Help would be highly appreciated!