首页 > 解决方案 > Xcode 编译器错误:编译器无法在合理的时间内对该表达式进行类型检查(Xcode 12.0 SwiftUI)

问题描述

(Xcode 12.0,SwiftUI)这段代码为我工作了将近 6 个月没有任何问题我昨天刚收到这个错误,没有任何编辑。我尝试了很多来清理代码并使其更短,但对我没有任何帮助。

GeometryReader { (geometry: GeometryProxy) in

  ForEach(0..<5) { index in

    Group {

      Circle()
        .foregroundColor(Color("GreyOne"))
        .frame(width: geometry.size.width / 5, height: geometry.size.height / 5)

        .scaleEffect(!self.isAnimating ? 1 - CGFloat(index) / 5 : 0.2 + CGFloat(index) / 5)

        .offset(y: geometry.size.width / 10 - geometry.size.height / 2)

      }.frame(width: geometry.size.width, height: geometry.size.height)

        .rotationEffect(!self.isAnimating ? .degrees(0) : .degrees(360))

        .animation(Animation
            
            .timingCurve(0.5, 0.15 + Double(index) / 5, 0.25, 1, duration: 1.5)

          .repeatForever(autoreverses: false))

    }

}.aspectRatio(1, contentMode: .fit)

    .onAppear {

      self.isAnimating = true

    }

标签: iosswiftswiftui

解决方案


您可以将您的部分内容移出View到单独的私有函数中,然后从body. 您的代码中有几个编译器错误,例如未传递xoffset函数或未将所有Ints 转换为CGFloat. 一旦你打破了body你的View,真正的错误就会浮出水面。

这是您的代码的编译版本,使用 2 个私有函数来构建body您的View.

struct YourView: View {
    @State private var isAnimating = false

    var body: some View {
        GeometryReader { geometry in
            ForEach(0..<5) { index in
                Group {
                    circle(geometry: geometry, index: index)
                }
                .frame(width: geometry.size.width, height: geometry.size.height)
                .rotationEffect(!self.isAnimating ? .degrees(0) : .degrees(360))
                .animation(animation(index: index))
            }
        }
        .aspectRatio(1, contentMode: .fit)
        .onAppear {
            self.isAnimating = true
        }
    }

    private func animation(index: Int) -> Animation {
        Animation
            .timingCurve(0.5, 0.15 + Double(index) / 5, 0.25, 1, duration: 1.5)
            .repeatForever(autoreverses: false)
    }

    private func circle(geometry: GeometryProxy, index: Int) -> some View {
        let width = geometry.size.width
        let height = geometry.size.height
        let yOffset = width / 10 - height / 2
        let nonAnimatingScale = 1 - CGFloat(index) / CGFloat(5)
        let animatingScale = 0.2 + CGFloat(index) / CGFloat(5)
        return Circle()
            .foregroundColor(Color("GreyOne"))
            .frame(width: width / 5, height: height / 5)
            .scaleEffect(self.isAnimating ? animatingScale : nonAnimatingScale)
            .offset(x: 0, y: yOffset)
    }
}

推荐阅读