尝试使用 forEach 创建多个视图会导致“静态方法‘buildExpression’要求‘Content’符合‘MapContent’”

问题描述 投票:0回答:1

在整个网络上搜索这个确切的错误并没有发现任何结果。我只想创建视图的多个副本,首尾相连,以创建类似于标尺的水平布局。在运行时,它将创建足够的段来覆盖特定的长度。

但是,我收到上述错误。将

id:\.self
添加到
forEach
并不能解决问题

import SwiftUI

struct MediaPositionView: View
{
    var body: some View
    {
        HStack(spacing: 0)
        {
            ForEach(0..<10)
            { index in
                songSegment(segIndex: index)    // Static method 'buildExpression' requires that 'Content' conform to 'MapContent'
            }
        }
    }

    @ViewBuilder func songSegment(segIndex: Int) -> any View
    {
        ZStack
        {
            Color.blue
                .frame(width: 50, height: 50)
            Path
            { path in
                path.move(to: CGPoint(x: 5, y: 25))
                path.addLine(to: CGPoint(x: 5, y: 50))
            }
        }
    }
}
swiftui foreach
1个回答
0
投票

函数

songSegment
需要返回
some View
,而不是
any View

func songSegment(segIndex: Int) -> some View
© www.soinside.com 2019 - 2024. All rights reserved.