简体   繁体   English

如何在 SwiftUI 中获取我的环境 object 数组的索引?

[英]How do I get the index of my environment object array in SwiftUI?

I am struggling to get the index of the object I am using in the for each loop.我正在努力获取我在 for each 循环中使用的 object 的索引。 I am trying to add a delete button next to each item in the array that removes the item from the object array.我正在尝试在数组中的每个项目旁边添加一个删除按钮,以从 object 数组中删除该项目。 So I would have a button inside the for each loop, and remove the item at that index.所以我会在 for each 循环中有一个按钮,并删除该索引处的项目。

Any help would be greatly appreciated:)任何帮助将不胜感激:)

import SwiftUI

struct AddedFoods:Identifiable{
    var name: String = ""
    var totalCals: Double = 0
    var totalProtein: Double = 0
    var totalCarbs: Double = 0
    var totalFat: Double = 0
    var id = UUID().uuidString
   //Your other properties
}


class FoodAddModel: ObservableObject,Identifiable {
    
    @Published var foods : [AddedFoods]?
    
    var id = UUID().uuidString

    init() {
        dummyData()
    }
    
    func dummyData() {
        var obj:[AddedFoods] = []
        obj.append(AddedFoods(name: "Pasta", totalCals: 340, totalProtein: 20, totalCarbs: 45, totalFat: 15))
        obj.append(AddedFoods(name: "Chicken", totalCals: 560, totalProtein: 20, totalCarbs: 45, totalFat: 15))
        obj.append(AddedFoods(name: "Apple", totalCals: 54, totalProtein: 20, totalCarbs: 45, totalFat: 15))
        obj.append(AddedFoods(name: "Noodles", totalCals: 231, totalProtein: 20, totalCarbs: 45, totalFat: 15))
        foods = obj
    }
}

struct myView:View{
    @EnvironmentObject var getFood:FoodAddModel
    @EnvironmentObject var person: UserInfoModel


    var unwrappedFoods:[AddedFoods]{
        getFood.foods ?? []
    }
    
    var body: some View{
        NavigationView{
            
        List{
        ForEach(unwrappedFoods) {obj in
            let b: String = String(obj.totalCals)
            
            List{
                HStack{
                        Text(obj.name)
                        Text(b)
                }
            }
        }.onDelete(perform: { indexSet in
            /*@START_MENU_TOKEN@*//*@PLACEHOLDER=Code@*/ /*@END_MENU_TOKEN@*/
        })

        }
    }
    }
    }

My favorite pattern for this is using enumerated() .我最喜欢的模式是使用enumerated() If you were doing this with a huge list, you may want to do some performance testing to see if it's doing extra work as opposed to just doing the for loop on the indexes, but for my purposes, it's worked pretty well:如果你用一个巨大的列表来做这个,你可能想要做一些性能测试,看看它是否在做额外的工作,而不是仅仅在索引上做 for 循环,但为了我的目的,它工作得很好:

ForEach(Array(unwrappedFoods.enumerated()), id: \.1.id) { (index, obj) in
            HStack {
                Text(obj.name)
                Spacer()
                Button(action: {
                    //delete with index
                }) {
                    Text("Delete")
                }
            }
        }

obj gives you the AddedFoods , just as before and index is (obviously) the index. obj像以前一样为您提供AddedFoods ,并且index (显然)是索引。

id has to use .1.id because .1 is now the second part of the tuple (in your case, the AddedFoods ). id必须使用.1.id因为.1现在是元组的第二部分(在您的情况下是AddedFoods )。

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

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