【发布时间】:2020-04-12 09:59:54
【问题描述】:
我尝试组合以下类型的两个对象
@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
感谢您的帮助!
【问题讨论】: