< How to fix “Ambiguous reference to member 'buildBlock()’” | How to fix “Property declares an opaque return type, but has no initializer expression from which to infer an underlying type” > |
Updated for Xcode 14.2
This error occurs for two reasons. First, you’ve created a view but didn’t actually return it from your view body, like this:
var body: some View {
let name = "Paul"
let message = Text("Hello, \(name)")
}
To make that compile, add return message
:
var body: some View {
let name = "Paul"
let message = Text("Hello, \(name)")
return message
}
The other common reason this happens is if you try to return two views without placing them in a container, like this:
var message: some View {
Text("Hello")
Text("World")
}
To fix this, place your views in a stack or a group, like this:
var message: some View {
VStack {
Text("Hello")
Text("World")
}
}
SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
Link copied to your pasteboard.