gpt4 book ai didi

arrays - SwiftUI 组合来自两个不同数组的两个 ObservedObjects

转载 作者:行者123 更新时间:2023-12-04 10:14:06 25 4
gpt4 key购买 nike

我尝试组合以下类型的两个对象

@ObservedObject var expenses = Expense()
@ObservedObject var recipes = Recipe()

阵列工作得很好,一切都很好。

现在我想在 ForEach 中显示数组中的所有项目。
    var body: some View {
TabView {
NavigationView {
List {
ForEach(Array(zip(expenses.items, recipes.ReItems)),id: \.0){ item in
HStack{
VStack(alignment: .leading){
Text(item.beschreibung)
.font(.headline)
Text(String(item.menge) + " \(item.unitType)")
}
}
}
.onDelete(perform: removeItems)
}

但这会引发错误 - “编译器无法在合理的时间内对该表达式进行类型检查;尝试将表达式分解为不同的子表达式”

我的第一个想法是将数组保存在这个 stackoverflow 帖子中的变量中
The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions
@State private var arrayExpense = self.expenses.items
@State private var arrayRecipes = self.recipes.ReItems

但老实说,这看起来不太好..它也会抛出异常;o

谢谢你的帮助!

最佳答案

尝试将其分解,如下所示(这给编译器明确的 zip 类型检查结果)

var body: some View {
TabView {
NavigationView {
List {
self.listContent(items: Array(zip(expenses.items, recipes.ReItems)))
}
...


private func listContent(items: [Item]) -> some View {
ForEach(items, id: \.0){ item in
HStack{
VStack(alignment: .leading){
Text(item.beschreibung)
.font(.headline)
Text(String(item.menge) + " \(item.unitType)")
}
}
}
.onDelete(perform: removeItems)
}

关于arrays - SwiftUI 组合来自两个不同数组的两个 ObservedObjects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61169658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com