Reputation: 511
While the following example is simple, it fails to compile because Swift cannot determine that the Text() view is the Content type required by the generic constraint (with error "Cannot convert value of type 'Text' to closure result type 'Content'").
Is there any way to have an init that can fulfil the generic constraint?
extension VStack {
init() {
self.init(spacing: 0) {
Text("Test")
}
}
}
The full example, after updating with George_E's reply, looks as follows.
extension VStack {
init<InnerContent: View>(alignment: HorizontalAlignment = .center, spacing: CGFloat? = nil, @ViewBuilder content: @escaping (GeometryProxy) -> InnerContent) where Content == GeometryReader<InnerContent> {
self.init(alignment: alignment, spacing: spacing) {
GeometryReader { proxy in
content(proxy)
}
}
}
}
Upvotes: 0
Views: 159
Reputation: 30461
You can include a where
clause for the init:
extension VStack {
init() where Content == Text {
self.init(spacing: 0) {
Text("Test")
}
}
}
Or:
extension VStack where Content == Text {
init() {
self.init(spacing: 0) {
Text("Test")
}
}
}
Upvotes: 1