Tuesday, October 4, 2022
HomeiOS Developmentios - Cannot assign to property: property is a get-only property?

ios – Cannot assign to property: property is a get-only property?

[ad_1]

I am trying bind a model data, Its list data of assets ,Everything is fine but when I am trying change the urgent which is enum, it shows enum is only get only property, Is there anyway I can make this variable bindable,

struct Asset : Identifiable {
    var id =  UUID()
   
    var urgentOptions : [HealthOptions] = []
 
    var isUrgent : ThreeStateBox   {
        let filtered = self.urgentOptions.filter { $0.isUrgentHealth == true}
        if filtered.count == 0 {
            return .none
        }
        if filtered.count == self.urgentOptions.count {
            return .all
        }
        if filtered.count > 0 && filtered.count < self.urgentOptions.count {
            return .partial
        }
        return .all
    }

enum ThreeStateBox {
    case all, partial, none
}

struct DropdownSelector: View {
    @Binding var topSelection: ThreeStateBox
    @State private var shouldShowDropdown = false
    @State private var selectedOption: DropdownOption? = nil

    var placeholder: String
    var options: [DropdownOption]
    var onOptionSelected: ((_ option: DropdownOption) -> Void)?
    var onDropdownSelected: (() -> Void)
    private let buttonHeight: CGFloat = 30

    var body: some View {
        VStack{
            Button(action: {
                self.shouldShowDropdown.toggle()
                onDropdownSelected()
            }) {
                HStack {
                    ThreeStateBoxView(state: $topSelection).frame(width: 14, height: 14, alignment: .leading)

I am trying to loop through the assets , and each item of assets has the value called isUrgent, which is an enum and want to bind that so that it changes it should automatically reflect, But it shows its only the get property, I had gone through the post SO link, but I could not manage to do it.

LazyVGrid(columns: gridItemLayout, spacing: 5) {
                        ForEach((0..<assets.count), id: \.self) { index in
                            let asset  = $assets[index]
                            HStack{
                                CheckBoxView(checked: asset.isEquipementCategory)
                                Text(asset.nameOfEquipmentCategory.wrappedValue).font(.caption).foregroundColor(Color.black)
                                Spacer()
                            }.padding(5)
                            HStack{
                                DropdownSelector(
                                    topSelection : asset.isUrgent,
                                    placeholder: "Urgent",
                                    options: options,
                                    onOptionSelected: { option in
                                        print(option)
                                    },
                                    onDropdownSelected: {
                                        eachCellStatus[index].isOpen.toggle()
                                        if ( eachCellStatus[index].isOpen ){
                                            let cellWithHighestZIndex = eachCellStatus.max(by: {$0.zIndex < $1.zIndex})
                                            eachCellStatus[index].zIndex = (cellWithHighestZIndex?.zIndex ?? 0) + 1
                                        }
                                    }
                                )

[ad_2]

Source link

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments