Saturday, October 1, 2022
HomeiOS Developmentios - Optional @State variable not initializing

ios – Optional @State variable not initializing

[ad_1]

I want to be able to open a sheet view where the user taps on something on a list, and all the information from that tapped list cell shows up on the sheet view. However, I am having a hard time trying to initialize the optional @State variable to the list value. This is the error I get:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

The State variable seems to remain empty, hence why the code is replying with it getting a nil value. How can I get an actual value to be inside so it’s not nil?

Any help would be appreciated.

My code is below:

struct ContentView: View {
    
    @Environment(\.managedObjectContext) var managedObjContext
    @FetchRequest(sortDescriptors: [SortDescriptor(\.date, order: .reverse)]) var counters: FetchedResults<Counter>
    
    @State private var creatingCounter = false
    @State private var editingCounter = false
    @State private var selectedCounter: FetchedResults<Counter>.Element? // The optional State var.
    
    var body: some View {
        NavigationView {
            VStack {
                List {
                    ForEach(counters) { counter in
                        TimelineView(.periodic(from: .now, by: 1.0)) { timeline in
                            CounterCellView(title: counter.title ?? "", icon: counter.icon ?? "", color: counter.color ?? "", date: counter.date ?? Date())
                        }
                        .listRowSeparator(.hidden)
                        .onTapGesture {
                            selectedCounter = counter // I want the state var to be initialized to the tapped item on the list.
                            editingCounter.toggle()
                        }
                    }
                    .onDelete(perform: deleteCounter)
                    
                }
                .listStyle(.inset)
            }
            .navigationTitle("Counters")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button {
                        creatingCounter.toggle()
                    } label: {
                        Image(systemName: "plus")
                    }
                }
            }
        }
        .sheet(isPresented: $creatingCounter) {
            CreateCounterView(creatingCounter: $creatingCounter)
        }
        .sheet(isPresented: $editingCounter) {
            EditCounterView(editingCounter: $editingCounter, counter: selectedCounter!) // Where I am getting the error.
        }
    }
    
    func deleteCounter(at offsets: IndexSet) {
        
        withAnimation {
            offsets.map { counters[$0] }.forEach(managedObjContext.delete)
            DataController().save(context: managedObjContext)
        }
    }
}

[ad_2]

Source link

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments