So the final code becomes:
#if os(iOS) || os(watchOS) || os(tvOS)
import UIKit
public protocol ViewRepresentable: UIViewRepresentable where UIViewType == ViewType {
associatedtype ViewType
associatedtype UIViewType = ViewType
@MainActor
func makeView(context: Context) -> ViewType
@MainActor
func updateView(_ view: ViewType, context: Context)
}
extension ViewRepresentable {
@MainActor
public func makeUIView(context: Context) -> UIViewType {
makeView(context: context)
}
@MainActor
public func updateUIView(_ uiView: UIViewType, context: Context) {
updateView(uiView, context: context)
}
}
#elseif os(macOS)
import AppKit
public protocol ViewRepresentable: NSViewRepresentable where NSViewType == ViewType {
associatedtype ViewType
associatedtype NSViewType = ViewType
@MainActor
func makeView(context: Context) -> ViewType
@MainActor
func updateView(_ view: ViewType, context: Context)
}
extension ViewRepresentable {
@MainActor
public func makeNSView(context: Context) -> NSViewType {
makeView(context: context)
}
@MainActor
public func updateNSView(_ nsView: NSViewType, context: Context) {
updateView(nsView, context: context)
}
}
#endif
By including where UIViewType == ViewType and where NSViewType == ViewType, I can prevent the user from specifying a different value for NSViewType/UIViewType than ViewType.
Thanks again.