8

我正在尝试在 SwiftUI 中创建一个带有两组十个按钮的 UI(想象一个 Cup Pong 游戏)。每当我尝试构建或预览代码时,都会收到以下错误消息:“编译器无法在合理的时间内对该表达式进行类型检查;尝试将表达式分解为不同的子表达式'。我想知道如何解决这个问题。

我知道它很长。有什么方法可以修复它,以便代码可以正常工作。

//  ContentView.swift
//  Text Pong
//
//  Created by Thomas Braun on 8/21/19.
//  Copyright © 2019 Thomas Braun. All rights reserved.
//

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack(spacing: 250.0) {//Contains both the triangles
            VStack {//User Triangle
                HStack(spacing: 15.0) {
                Button(action: {}) {
                        Text("7")
                            .font(.largeTitle)
                    }
                Button(action: {}) {
                        Text("8")
                            .font(.largeTitle)
                    }
                Button(action: {}) {
                        Text("9")
                            .font(.largeTitle)
                    }
                Button(action: {}) {
                        Text("10")
                            .font(.largeTitle)
                    }
                }
                HStack(spacing: 15.0) {
                Button(action: {}) {
                        Text("6")
                            .font(.largeTitle)
                    }
                Button(action: {}) {
                        Text("5")
                            .font(.largeTitle)
                    }
                Button(action: {}) {
                        Text("4")
                            .font(.largeTitle)
                    }
                }
                HStack(spacing: 15.0) {
                Button(action: {}) {
                        Text("3")
                            .font(.largeTitle)
                    }
                Button(action: {}) {
                        Text("2")
                            .font(.largeTitle)
                    }
                }
                HStack(spacing: 15.0) {
                Button(action: {}) {
                        Text("1")
                            .font(.largeTitle)
                    }
                }
            }

            //            Text("Game On")

            VStack {//Opponent Triangle
                HStack {
                    VStack {
                Button(action: {}) {
                            Text("1")
                                .font(.largeTitle)
                        }
                        HStack {
                Button(action: {}) {
                                Text("2")
                                    .font(.largeTitle)
                            }
                Button(action: {}) {
                                Text("3")
                                    .font(.largeTitle)
                            }
                        }
                        HStack {
                            Button(action: {}) {
                                Text("4")
                                    .font(.largeTitle)
                            }
                            Button(action: {}) {
                                Text("5")
                                    .font(.largeTitle)
                            }
                            Button(action: {}) {
                                Text("6")
                                    .font(.largeTitle)
                            }
                        }
                        HStack {
                            Button(action: {}) {
                                Text("7")
                                    .font(.largeTitle)
                            }
                            Button(action: {}) {
                                Text("8")
                                    .font(.largeTitle)
                            }
                            Button(action: {}) {
                                Text("9")
                                    .font(.largeTitle)
                            }
                            Button(action: {}) {
                                Text("10")
                                    .font(.largeTitle)
                            }
                        }
                    }
                }


            }// Ending Opponent Triangle verticle Stack
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
4

3 回答 3

12

把它分成更小的部分。例如,按每一行,然后按每个玩家,如下所示:

struct OpponentTriangleView: View {
    var body: some View {
        VStack {//Opponent Triangle
            HStack {
                VStack {
                    Part1View()
                    Part2View()
                    Part3View()
                    Part4View()
                }
            }
        }// Ending Opponent Triangle vertical Stack
    }
}

并像这样定义每个部分:

extension OpponentTriangleView {
    struct Part1View: View {
        var body: some View {
            HStack {
                Button(action: {}) { Text("1") .font(.largeTitle) }
            }
        }
    }

    struct Part2View: View {
        var body: some View {
            HStack {
                Button(action: {}) { Text("2").font(.largeTitle) }
                Button(action: {}) { Text("3").font(.largeTitle) }
            }
        }
    }

    struct Part3View: View {
        var body: some View {
            HStack {
                Button(action: {}) { Text("4").font(.largeTitle) }
                Button(action: {}) { Text("5").font(.largeTitle) }
                Button(action: {}) { Text("6").font(.largeTitle) }
            }
        }
    }

    struct Part4View: View {
        var body: some View {
            HStack {
                Button(action: {}) { Text("7").font(.largeTitle) }
                Button(action: {}) { Text("8").font(.largeTitle) }
                Button(action: {}) { Text("9").font(.largeTitle) }
                Button(action: {}) { Text("10").font(.largeTitle) }
            }
        }
    }
}

同样定义UsertTriangleView. 然后像这样使用它们:

struct ContentView: View {
    var body: some View {
        VStack(spacing: 250.0) {//Contains both the triangles
            UserTriangleView()
            //            Text("Game On")
            OpponentTriangleView()
        }
    }
}

你可以走了 工作预览

- 注意事项:

  1. 不仅在 SwiftUI 中,而且总是将大量代码分解成更小的有意义的部分。
  2. 不要重复自己。尝试创建一些构建器函数或使用循环来实现重复任务,而无需一次又一次地实际编写它。
于 2019-08-22T05:59:16.680 回答
2

当我的视图中出现构建错误时,我已经看到 Xcode (12.5.1) 显示了这一点。尝试注释掉部分视图并重建,它最终会告诉你真正的错误是什么。就我而言,我缺少一个子视图的构造函数参数。在你找到真正的问题并修复它之后,你可以取消所有的注释,它现在应该可以正常构建了。

于 2021-07-29T15:36:35.673 回答
0

就我而言,很多 @State 是导致此错误的原因。删除一个后它开始工作

于 2021-04-13T03:15:30.990 回答