Home > Blockchain >  Error trying to save to core data in SwiftUI
Error trying to save to core data in SwiftUI

Time:12-15

I have 2 custom radio buttons. When i click on one of them, i want them to save to a core data variable, but i got this error. " Cannot assign to value: 'selected' is a 'let' constant"

This is how i'm calling the radio buttons :

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

This is my structs for the radio buttons :

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)"
}

How can i fix this issue ? Secondly, is this a good way to store data to core data ? Thanks !

CodePudding user response:

selected = coreDataViewModel.savedCart[0].paymentMethod

is trying to assign a value from your model to selected, which is a constant. Shouldn't it be

coreDataViewModel.savedCart[0].paymentMethod = selected

In terms of your second question, it's hard to say given the information you've provided. One thing I would advise is to use enums with string raw values for things like payment methods, and have calculated properties on your managed object models to access them so you're not passing strings around:

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