简体   繁体   English

如何在 Swift 中的结构数组上使用 ForEach?

[英]How to use ForEach on an array of struct in Swift?

I have an array of Struct我有一个结构数组

struct StartView: View {
    struct ExampleStruct {
        var name: String
        var keywords: String
        var destinationID: String
        
        init(name: String, keywords: String, destinationID: String) {
            self.name = name
            self.keywords = keywords
            self.destinationID = destinationID
        }
    }

// Created 3 examplmes
        let AStruct = ExampleStruct(name: "Structure A",keywords: "first: school",destinationID: "SAID")
        let BStruct = ExampleStruct(name: "Structure B",keywords: "second: church",destinationID: "SBID")
        let CStruct = ExampleStruct(name: "Structure C",keywords: "third: bank",destinationID: "SCID")
    }

// Created my array of structures
    var StructureArray = (AStruct, BStruct, CStruct)

However, I am now trying to create a NavigationView with a List but I am having issues getting this to work.但是,我现在正在尝试创建一个带有ListNavigationView ,但是我在让它工作时遇到了问题。

    var body: some View {
            NavigationView{
                List {
                    for index in StructureArray.indices {
                        VStack {
                           // Text to put ExampleStruct name
                           // Text to put ExampleStruct keywords
                           // Text to put ExampleStruct destinationID
                        }
                        .padding()
                    }
                }
                .navigationTitle("Structure Search")
            }
        }

I get a Closure containing control flow statement cannot be used with result builder 'ViewBuilder' based on the for index in StructureArray.indices根据for index in StructureArray.indices我得到一个Closure containing control flow statement cannot be used with result builder 'ViewBuilder'

I have also tried:我也试过:

for index in 0..<StructureArray.count {
                        let currentStruc = StructureArray[index]
                        HStack {
                            Text (currentStruc.name)
                            Text (currentStruc.keywords)
                            \\ etc
                        }

But get the same error.但是得到同样的错误。 I have searched online for the last few hours and am still lost on how to make this work.我在网上搜索了几个小时,但仍然不知道如何进行这项工作。 Am I missing something obvious?我错过了一些明显的东西吗? Would a ForEach be better? ForEach 会更好吗?

You can try like below.你可以像下面这样尝试。 I have corrected a few of your code.我已经更正了您的一些代码。 Google the changes to better understand it.谷歌更改以更好地理解它。

ExampleStruct示例结构

struct ExampleStruct: Identifiable {
    let id = UUID()
    var name: String
    var keywords: String
    var destinationID: String
}

ContentView内容视图

import SwiftUI

struct ContentView: View {
    
    let aStruct = ExampleStruct(name: "Structure A",keywords: "first: school",destinationID: "SAID")
    let bStruct = ExampleStruct(name: "Structure B",keywords: "second: church",destinationID: "SBID")
    let cStruct = ExampleStruct(name: "Structure C",keywords: "third: bank",destinationID: "SCID")
    
    var structArray: [ExampleStruct] {
        return [aStruct, bStruct, cStruct]
    }
    
    var body: some View {
        List {
            ForEach(structArray) { item in
                Text(item.name)
            }
        }
    }

}

Playgrounds test游乐场测试

证据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM