I am currently learning SwiftUI, and I ran into a problem.
In the below code I have two initialisers. The first one works (assuming I comment out the second one long enough to test the code). The second one does not. It throws a compiler error saying "Cannot assign value of type 'some View' to type 'Content'."
What am I doing wrong here? I am able to successfully call the first initialiser from outside the struct using Infobox() { Text("Some Text") }, which seems to me to be the exact same syntax I'm using when trying to call it from the second initialiser. No matter how much I google, I can't seem to figure this one out.
I'd very much appreciate any help you can give.
struct Infobox<Content>: View where Content: View {
let content: Content
var body: some View {
content
}
init(@ViewBuilder _ content: @escaping () -> Content) {
self.content = content()
}
init(withHeader headerText: String, @ViewBuilder _ content: @escaping () -> Content) {
self.init() {
VStack(alignment: .leading) {
Text(headerText)
.font(.headline)
}
content()
}
}
}
}