Delegate returning nil value

I have tried to use a delegate to talk between different SKScenes and for some reason am returning a nil value when called:

protocol RemoveListEntry : class {
    func removeItemInList()
}


class List {

    func removeItemInList() {
        print("remove item")
    }
}

class Menu {

    var entryRemovalDelegate : RemoveListEntry!
    
    
    let listEntryRemoval: Button = {

        let button = Button(imageNamed: "button", title: "Remove Entry", buttonAction: {
            let listEntryRemoval = Menu()
            listEntryRemoval.removeEntry()
        })
        button.position = CGPoint(x: 0.5, y: 0.5)
        button.name = "listEntryRemoval"
        return button
    }()
    
    
    func removeEntry() {
        
        print("value: \(entryRemovalDelegate)")         //why is this nil, it will not pass ?
        if let removeEntry = entryRemovalDelegate {
            removeEntry.removeItemInList()
        }
    }
}

The idea is to click a button and have it execute on another scene, but the "entryRemovalDelegate" is a nil value, and so the function is not called.

I think your delegate is supposed to be weak (and then you can also skip the if let check). Also, can you double check if you’re actually setting the delegate property?

Terms of Service

Privacy Policy

Cookie Policy