Re: Safe cross references between scenes in an OS X storyboard
Re: Safe cross references between scenes in an OS X storyboard
- Subject: Re: Safe cross references between scenes in an OS X storyboard
- From: Roland King <email@hidden>
- Date: Wed, 09 Mar 2016 21:42:02 +0800
> On 9 Mar 2016, at 21:32, Bill Cheeseman <email@hidden> wrote:
>
> I am using storyboards in an OS X application written in Swift 2. It is a single window "shoebox" application that does not use NSDocument or Core Data. It is built with the Xcode 7.2.1 storyboard template, where the storyboard entry point is the main window controller scene and a "window content" relationship segue leads to the window's content view scene.
>
> In my AppDelegate class, I want to declare an instance property referring to the main window's content view controller. I need to refer to the content view controller because my AppDelegate provides programmatic content for one of the menu bar's menus and it needs to update a duplicate of that menu in a popup button in the content view. I anticipate needing a reference to the content view controller for other purposes, too, so I want to avoid duplicating code by putting it in an instance variable.
>
> It is of course impossible to set the property's value before the window's content view controller exists. The applicationDidFinishLaunching(_:) delegate method is apparently too early. If I set it there with 'mainContentViewController = NSApp.mainWindow!.contentViewController as! MainContentViewController', I get this fatal error at launch: "unexpectedly found nil while unwrapping an Optional value."
>
> I therefore set it using a lazy instance variable, like this:
>
> lazy var mainContentViewController: MainContentViewController = {
> NSApp.mainWindow!.contentViewController as! MainContentViewController
> }()
>
> This works fine -- but only if I take care to use the instance variable after the content view controller becomes available. My question is whether the following is a safe and sensible way to take care of the possibility that I might accidentally try to use it too early:
>
> lazy var mainContentViewController: MainContentViewController? = {
> guard let window = NSApp.mainWindow,
> let controller = window.contentViewController
> else {return nil}
> return controller as? MainContentViewController
> }()
>
> (I tried returning 'controller as! MainContentViewController', but I got this warning: "Treating a forced downcast to 'MainContentViewController' as optional will never produce 'nil'." I have to declare the variable as an Optional type because the guard statement might return nil.)
>
> —
just
return NSApp.mainWIndow?.contentViewController as? MainContentViewController
works without all the guard and intermediate variables and still returns the same optional.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden