> On 9 Mar 2016, at 21:32, Bill Cheeseman <wjcheese...@gmail.com> 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 (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to