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: Bill Cheeseman <email@hidden>
- Date: Thu, 10 Mar 2016 06:10:47 -0500
> On Mar 9, 2016, at 7:22 PM, Quincey Morris <email@hidden> wrote:
>
> No, it’s *really* a property, so like this:
>
>> class MainContentViewController: NSWindowController {
>>
>> static private(set) var controller: MainContentViewController! // defaults to nil until set
>>
>> override func windowDidLoad () {
>> precondition (MainContentViewController.controller== nil)
>> MainContentViewController.controller = self
>> }
>> }
>
> It’s a “regular” stored property, so you don’t have to write a setter or getter. (Your original version really is recursive.)
>
> I’ve declared the property “private(set)” so that no one outside the class can change it, and I’ve added a check that there’s only one MainContentViewController ever created.
This works like a charm. It's the best bit of magic I've encountered in a long time. Now that you lay it out for me, it seems obvious and it feels very natural.
For storyboard relationship segues, it seems like this should be a standard technique, instead of my original plan to declare an instance variable in every class that needs access to this controller and struggle to find some way to set its value. Type variables promise to be a very powerful feature of Swift. It will be hard to go back to Objective-C.
--
Bill Cheeseman - email@hidden
_______________________________________________
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