TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Assign a value to an optional when sheet is presented

Forums > SwiftUI

Hi guys,

can anyone help me with the following.

I have declared an optional Allocation. When I tap on the plus button the function generateNewAllocation create a new one from the default and then assign it to the optional allocation.

The problem is that the first time I tap on the + button it would assign nil as it presents a blank sheet, but if I dismiss it and tap again the value is correctly assigned.

I have checked the function which correctly returns the expected allocation, but can't figure out what is going wrong.


import SwiftUI
import SwiftData

struct AllocationsView2: View {
    @Environment(\.modelContext) var modelContext
    @Environment(\.dismiss) var dismiss

    @State private var showEmployees = false
    @State private var allocation: Allocation?
    @State private var isSaved = false

    @Query(filter: #Predicate<Allocation> {allocation in
        allocation.isSetup == false
    }) var allocations: [Allocation]

    var body: some View {
        NavigationStack {
            List {
           /*if let unwrappedAllocation = allocation {

                    ForEach(unwrappedAllocation.areas) {area in

            .navigationTitle("Allocations List")
                Button(action: {

                   modelContext.autosaveEnabled = false
                   let newAllocation = generateNewAllocation()
                    allocation = newAllocation
                    showEmployees = true

                }) {
                    Image(systemName: "plus")
                .sheet(isPresented: $showEmployees) {
                   if  let currentAllocation = allocation {
                        Employees(allocation: currentAllocation, dismissView: $showEmployees, isSaved: $isSaved)




    func generateNewAllocation() -> Allocation? {
        var fetchDescriptor = FetchDescriptor<Allocation>(predicate: #Predicate  { allocation in
            allocation.isSetup == true
        fetchDescriptor.fetchLimit = 1
        do {
            let result = try modelContext.fetch(fetchDescriptor)
            let allocationFirst = result.first

            let newAllocation = Allocation(id: UUID(), date: .now, shift: "Short Day", isSetup: false)
            if let areas = allocationFirst?.areas {
                for area in areas {
                    let newArea = Area(id: UUID(), name:, order: area.order, priority: area.priority, isActive: area.isActive)
                   // print(
            for area in newAllocation.areas {
            return newAllocation

        } catch {
            print("failed to load Default Allocation")
            return nil



Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.