SwiftUI - Xcode - VStack 无法推断类型

SwiftUI - Xcode - Inferring type not possible for VStack

我正在尝试在 Xcode 中创建一个简单的 master/detail 应用程序。

我希望详细视图是

struct EditingView: View
{
    var body: some View {
        var mainVertical: VStack = VStack() //error here
            {
                var previewArea: HStack = HStack()
                {
                    var editorButton: Button = Button()
                    //the same with return editorButton
                    // I have to add other controls, like a WKWebView
                }
                return previewArea
                //this is a simple version, layout will have other stacks with controls inside
        }
        return mainVertical
    }
}

但我明白了

Generic parameter 'Content' could not be inferred

IDE 让我修复,但如果我这样做,它会写一个我必须填写的通用类型,但随后会出现其他错误,f.i。如果我把 AnyView 或 TupleView.

我希望它能推断一切,它有什么不明白的?

在 SwiftUI 中,您通常不需要引用控件。您可以直接在视图中对它们应用修饰符。

这是首选方式:

struct ContentView: View {
    var body: some View {
        VStack {
            HStack {
                Button("Click me") {
                    // some action
                }
            }
        }
        .background(Color.red) // modify your `VStack`
    }
}

或者,如果需要,您可以将控件提取为单独的变量:

struct ContentView: View {
    var body: some View {
        let hstack = HStack {
            button
        }
        return VStack {
            hstack
        }
    }

    var button: some View {
        Button("Click me") {
            // some action
        }
    }
}

但最后我绝对推荐你阅读Apple SwiftUI tutorials