首页 > 解决方案 > 从 3 个数组中获取值,以便在 ForEach 中使用

问题描述

我有三个数组,我想从所有数组中获取以显示给用户。当我有两个数组时,我在下面找到了这段代码,它运行良好

            List(Array(zip(book.words, book.definitions)), id: \.self.0) { (word, definition) in
                HStack {
                    Text("\(word) - \(definition)")
                    Spacer()
                    Button(action: {
                        textToSpeech(word)
                    }) {
                        Image(systemName: "speaker.3")
                    }.buttonStyle(PlainButtonStyle())
                }
            }

我不能向它添加另一个数组,因为它是不允许的。我试图根据数组中的计数来确定它,因为它们的大小都相同。

            List {
                ForEach(0 ..< book.words.count, id: \.self) { index in
                    HStack {
                        Text("\(book.words[index]) (\(book.partOfSpeech[index])) - \(book.definitions[index])")
                        Spacer()
                        Button(action: {
    //                        print("hello")
                            textToSpeech(book.words[index])
                        }) {
                            Image(systemName: "speaker.3")
                        }.buttonStyle(PlainButtonStyle())
                    }
                }
            }

但是我得到了这个错误

Swift/ContiguousArrayBuffer.swift:580: Fatal error: Index out of range
2021-07-19 19:43:31.572442-0700 SQLBook[61246:3585835] Swift/ContiguousArrayBuffer.swift:580: Fatal error: Index out of range

变量 book 是一个类,它被传入并用 @ObservedObject 修饰。我不确定我正在尝试做的事情是否可行,因为我看到人们问这个问题但从未得到回应。

标签: arraysswiftui

解决方案


每当您处理数组时,最好检查您要使用的索引是否存在。我怀疑你 3 个数组的大小不同。我通过以下测试成功测试了您的代码:

import SwiftUI

@main
struct TestApp: App {
    @StateObject var book = BookModel()
    var body: some Scene {
        WindowGroup {
            ContentView(book: book)
        }
    }
}

class BookModel: ObservableObject {
    @Published var words = ["word1","word2","word3"]
    @Published var partOfSpeech = ["pos1","pos2","pos3"]
    @Published var definitions = ["def1","def2","def3"]
}

struct ContentView: View {
    @ObservedObject var book: BookModel
    
    var body: some View {
        VStack(alignment: .leading ){
            if (book.words.count == book.partOfSpeech.count && book.words.count == book.definitions.count) {
                List {
                    ForEach(book.words.indices, id: \.self) { index in
                        HStack {
                            Text("\(book.words[index]) (\(book.partOfSpeech[index])) - \(book.definitions[index])")
                            Spacer()
                            Button(action: {
                                textToSpeech(book.words[index])
                            }) {
                                Image(systemName: "speaker.3")
                            }.buttonStyle(PlainButtonStyle())
                        }
                    }
                }
            } else {
                Text("arrays not the same size") // show somehting else
            }
        }
    }
    
    func textToSpeech(_ word: String) {
        print("---> in textToSpeech word: " + word)
    }
}

推荐阅读