Hello,
I have a generic class and am missing something. I have the following class:
struct TitleContainer<TitleContent, SubtitleContent> {
let titleContent: TitleContent
let subtitleContent: SubtitleContent?
init(_ titleContent: TitleContent, _ subtitleContent: SubtitleContent?) {
self.titleContent = titleContent
self.subtitleContent = subtitleContent
}
var description: String {
"title: “\(titleContent)”\(subtitleContent != nil ? "subtitle: “\(subtitleContent!)”" : "")"
}
func apply(_ popupItem: LNPopupItem, popupBar: LNPopupBar) {
fatalError("Unsupported type")
}
}
extension TitleContainer where TitleContent == String, SubtitleContent == String {
func apply(_ popupItem: LNPopupItem, popupBar: LNPopupBar) {
popupItem.title = titleContent
popupItem.subtitle = subtitleContent
}
}
@available(iOS 15, *)
extension TitleContainer where TitleContent == AttributedString, SubtitleContent == AttributedString {
func apply(_ popupItem: LNPopupItem, popupBar: LNPopupBar) {
popupItem.attributedTitle = titleContent.swiftUIToUIKit
popupItem.attributedSubtitle = subtitleContent?.swiftUIToUIKit
}
}
extension TitleContainer where TitleContent: View, SubtitleContent: View {
func apply(_ popupItem: LNPopupItem, popupBar: LNPopupBar) {
let subtitleView: AnyView
if let subtitleContent {
subtitleView = AnyView(subtitleContent)
} else {
subtitleView = AnyView(EmptyView())
}
let titleView = TitleContentView(titleView: AnyView(titleContent), subtitleView: subtitleView, popupBar: popupBar)
popupItem.setValue(LNPopupBarTitleViewAdapter(rootView: titleView).view, forKey: "swiftuiTitleContentView")
}
}
I have an outer class that uses the above class in the following way:
struct PopupItem<TitleContent, SubtitleContent, ButtonToolbarContent: ToolbarContent>: Identifiable {
public
let id: String
let titleContainer: TitleContainer<TitleContent, SubtitleContent>
//...
func apply(_ popupItem: LNPopupItem, popupBar: LNPopupBar) {
//...
titleContainer.apply(popupItem, popupBar: popupBar)
//...
}
func lnPopupItem(for popupBar: LNPopupBar) -> LNPopupItem {
let rv = LNPopupItem()
apply(rv, popupBar: popupBar)
return rv
}
}
At runtime, it always goes to the fatalError variant above.
Leaving only the String variant of the apply method, the compiler complaints that
Referencing instance method 'apply(_:popupBar:)' on 'TitleContainer' requires the types 'SubtitleContent' and 'String' be equivalent
But if I put a breakpoint on the fatalError line, in the debugger I see:
(lldb) po type(of: self)
LNPopupUI.TitleContainer<Swift.String, Swift.String>
(lldb) po type(of: self)
LNPopupUI.PopupItem<Swift.String, Swift.String, SwiftUI.TupleToolbarContent<SwiftUI.ToolbarItem<(), SwiftUI.Button<SwiftUI.Text>>>>
So types are correct at runtime, but the compiler is confused.
Any assistance will be appreciated!