Search code examples
swiftxcodeuiscenedelegate

Cannot access SceneDelegate class variable/property


I'm trying to access a variable declared in the SceneDelegate, however, I can't access it. My code in my viewcontroller is as:

let sceneDelegate = self.view.window.windowScene.delegate
let variableToAccess = sceneDelegate.variableToAccess

I thought that would do it.. not sure what I'm missing? Thanks


Solution

  • The property you are trying to access is of type UISceneDelegate. So to get the value you need to cast the delegate property to SceneDelegate like this:

    if let sceneDelegate = self.view.window.windowScene.delegate as? SceneDelegate {
      let variableToAccess = sceneDelegate.variableToAccess
      print(variableToAccess)
    }