> On Mar 9, 2016, at 7:22 PM, Quincey Morris 
> <quinceymor...@rivergatesoftware.com> 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 - wjcheese...@comcast.net

_______________________________________________

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