I want to make a secondary array into a primary array using the for-in syntax and process it

Asked 2 years ago, Updated 2 years ago, 106 views

Currently, I want to process the expression in the secondary array as a primary array using the for-in syntax, but when I try to process it,

Close containing control flow statement cannot be used with function builder 'ViewBuilder'

An error such as this occurs in the for-in syntax and cannot be handled successfully.What should I do?

code (sample)

import SwiftUI

structure Author:Identify {
  variable —Int
  var name —String
}

structure sample:View {
    var something: [[Author]] = [[Author (id:1, name: "Hanako"), Author (id:2, name: "Hiroshi")], Author (id:3, name: "Takashi")]]

    varbody:someView {
        VStack {
            for one in something {←Error here
                for one in some {     
                    Text("\(one.name")")
                }
          Divider()
            }
        }
    }
}

swift swiftui

2022-09-29 22:55

1 Answers

Closure arguments passed to VStack and so on are special, called @ViewBuilder, and cannot contain regular for statements.

Use the ForEach structure to represent simple iterations.

For example, you can write like this

 structure Sample: View {
    var something: [[Author]] = [[Author (id:1, name: "Hanako"), Author (id:2, name: "Hiroshi")], Author (id:3, name: "Takashi")]]

    varbody:someView {
        VStack {
            ForEach (something.indices) {index in)
                ForEach(self.something [index]) {one in
                    Text("\(one.name")")
                }
                Divider()
            }
        }
    }
}


2022-09-29 22:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.