如何编辑 ForEach 循环内的变量

问题描述 投票:0回答:1

我这里有这个代码:

struct CheckoutView: View {

    var itemInfoList: [itemInfo]
    @State private var mutableItemInfoList: [itemInfo] = []
    
    init(itemInfoList: [itemInfo]) {
        self.itemInfoList = itemInfoList
        self.mutableItemInfoList = itemInfoList
    }
    
    var body: some View {
        ForEach(itemInfoList, id: \.self) { newItem in
       
            Button(action: {
                newItem.size = selectedSizesString
                // problematic line
            }) {
                Text("Print Selected Sizes")
            }
            .alert(isPresented: $showingAlert) {
                Alert(
                    title: Text("Selected Sizes"),
                    message: Text(selectedSizesString),
                    dismissButton: .default(Text("OK"))
                )
            }
        }
    }
}

struct itemInfo: Hashable {
    var item: Item
    var size: String
    var comment: String
}

我正在尝试编辑每个项目内部的尺寸属性,但出现此错误:>

Cannot assign to property: 'newItem' is a 'let' constant

我不确定我做错了什么,我最好的猜测是

item
ForEach
循环内。

swiftui foreach
1个回答
0
投票

像这样:

ForEach($items) { $item in
© www.soinside.com 2019 - 2024. All rights reserved.