You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI编译器无法类型检查表达式,如何改写ForEach相关代码?

SwiftUI 编译器类型检查超时问题:镂空圆形蒙版实现失败

我想用ForEach循环创建镂空圆形蒙版来实现穿孔边缘效果,结果碰到了编译器错误:

The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions

之前遇到这类错误时,按提示拆分表达式就能解决,但这次我找不到可行的拆分方式。

我怀疑问题出在Circle使用holePunchSize设置的padding上——换成固定值的话代码就能正常运行。最开始我是把乘法表达式直接写在.padding里:((geo.size.width - 40) / 17) * 2.3。后来改成在.onAppear里分步计算holePunchSize再引用,甚至把计算步骤拆得更细(就是下面.onAppear里的写法),但还是报错,实在不知道还能怎么拆。

另外我也试过简化代码测试:把.frame(width..)和除一个外的所有.padding改成固定值,编译器还是报错。

相关代码

.reverseMaskBottomLeading {
    ForEach(0..<17) { i in
        Circle()
            .frame(width: (geo.size.width - 40) / 17, alignment: .bottomLeading)
            .padding([.bottom], -14)
            .padding([.leading], i * (geo.size.width - 40) )
            .padding([.leading], CGFloat((i * (holePunchSize))))
            .padding([.leading], -12)
    }
    .padding([.leading, .trailing])
}
.onAppear {
    holePunchSize = geo.size.width - 40
    holePunchSize = holePunchSize / 17
    holePunchSize = holePunchSize * 2.3
}


...


@inlinable
public func reverseMaskBottomLeading<Mask: View>(
    alignment: Alignment = .bottomLeading,
    @ViewBuilder _ mask: () -> Mask
) -> some View {
    self.mask {
        Rectangle()
            .overlay(alignment: alignment) {
                mask()
                    .blendMode(.destinationOut)
            }
    }
}

内容的提问来源于stack exchange,提问作者Joseph

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 20:20:26