尝试保存到 SwiftUI 中的核心数据时出错

Error trying to save to core data in SwiftUI

我有 2 个自定义单选按钮。当我单击其中一个时,我希望将它们保存到核心数据变量中,但出现此错误。 “无法赋值:'selected' 是一个 'let' 常量”

这就是我调用单选按钮的方式:

RadioButtonGroups { selected in // <- Error here 
                        print("Selected payment is: \(selected)")
                        selected = coreDataViewModel.savedCart[0].paymentMethod
                        coreDataViewModel.manager.save()
                    }

这是我的单选按钮结构:

struct RadioButtonField: View {
    let id: String
    let label: String
    let size: CGFloat
    let img : String
    let color: Color
    let textSize: CGFloat
    let isMarked:Bool
    var callback: (String)->()
    init(
        id: String,
        label:String,
        img : String,
        size: CGFloat = 20,
        color: Color = Color.colorGrayDark,
        textSize: CGFloat = 16,
        isMarked: Bool = false,
        callback: @escaping (String)->()
    ) {
        self.id = id
        self.label = label
        self.size = size
        self.color = color
        self.textSize = textSize
        self.isMarked = isMarked
        self.callback = callback
        self.img = img
    }
    
    var body: some View {
        Button(action:{
            self.callback(self.id)
        }) {
            HStack(alignment: .center, spacing: 10) {
                Image(img, bundle: Bundle.main)
                Text(label)
                    .font(Font.system(size: textSize))
                Spacer()
                Image(self.isMarked ? "checkboxSelected" : "checkboxUnselected")
                    .renderingMode(.original)
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width: self.size, height: self.size)
                //                    Spacer()
            }.foregroundColor(self.color)
        }
        .foregroundColor(Color.white)
    }
    
}


struct RadioButtonGroups: View {
    var callback: (String) -> ()
    
    @State var selectedId: String = ""
    
    var body: some View {
        VStack {
            radioCash
            radioCard
        }
    }
    
    var radioCash: some View {
        RadioButtonField(
            id: Payment.cash.rawValue,
            label: Payment.cash.rawValue, img: "cash",
            isMarked: selectedId == Payment.cash.rawValue ? true : false,
            callback: radioGroupCallback
        )
    }
    
    var radioCard: some View {
        RadioButtonField(
            id: Payment.card.rawValue,
            label: Payment.card.rawValue, img: "card",
            isMarked: selectedId == Payment.card.rawValue ? true : false,
            callback: radioGroupCallback
        )
    }
    
    func radioGroupCallback(id: String) {
        selectedId = id
        callback(id)
    }
}

enum Payment: String {
    case cash = "Cash"
    case card = "Card (la livrare)"
}

我该如何解决这个问题? 其次,这是将数据存储到核心数据的好方法吗? 谢谢!

selected = coreDataViewModel.savedCart[0].paymentMethod

正在尝试将模型中的值分配给 selected,这是一个常量。不应该

coreDataViewModel.savedCart[0].paymentMethod = selected

关于你的第二个问题,根据你提供的信息,很难说。我建议的一件事是使用带有字符串原始值的枚举来表示支付方式,并在您的托管对象模型上计算属性以访问它们,这样您就不会传递字符串:

var paymentMethod: PaymentMethod {
    get { PaymentMethod(rawValue: rawPaymentMethod) ?? .turnips }
    set { rawPaymentMethod = newValue.rawValue }
}