Re: Subclassing NSView in Yosemite?

2014-11-06 Thread Kyle Sluder
On Nov 6, 2014, at 6:29 AM, Alex Hall  wrote:
> 
> Hello list,
> I've subclassed NSView before, doing all my setup in init withFrame, and it 
> works fine. Now, though, I'm using Swift in Xcode 6.1 to build a project 
> against 10.10, and I'm getting an error that I need to implement 
> initWithCoder:NSCoder.
> 
> The problem is that the documentation says nothing about this, so I'm not 
> sure which initializer is called when. That is, where do I put all my setup 
> to ensure it gets called? I'd rather not do it twice, since I'm afraid one or 
> the other initializer could get called when I don't expect it and set things 
> up as though the app had just launched.

Read the Resource Programming Guide. -initWithCoder: is primarily called when 
unarchiving a view from a nib.

It’s kind of annoying that Swift makes you implement this method for all views, 
regardless of whether they’ll be in a nib or not, but it’s the only way the 
language can back up its safety guarantees.

--Kyle Sluder
___

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

Re: Subclassing NSView in Yosemite?

2014-11-06 Thread David Duncan

> On Nov 6, 2014, at 6:29 AM, Alex Hall  wrote:
> 
> Hello list,
> I've subclassed NSView before, doing all my setup in init withFrame, and it 
> works fine. Now, though, I'm using Swift in Xcode 6.1 to build a project 
> against 10.10, and I'm getting an error that I need to implement 
> initWithCoder:NSCoder.

NSView conforms to NSCoder, which is where this initializer comes from. It is 
generally used for unarchiving from a nib(xib) or storyboard.

> The problem is that the documentation says nothing about this, so I'm not 
> sure which initializer is called when. That is, where do I put all my setup 
> to ensure it gets called? I'd rather not do it twice, since I'm afraid one or 
> the other initializer could get called when I don't expect it and set things 
> up as though the app had just launched.
> 
> I can't find much of anything on this initWithCoder requirement, so I'm at a 
> loss as to where to set things up, and even what to pass the initializer for 
> an NSCoder. I'd appreciate any links or other resources, or just 
> explanations, anyone can provide. Thanks. Oh, here's the page I'm looking at, 
> among many others on Google:
> https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/index.html#//apple_ref/doc/uid/2014-SW10
>  
> 

Typically you don’t call -initWithCoder:, except to call super implementations. 
Swift will take care of the latter for you, so you would just need to decode 
whatever information you need. Of course if your view doesn’t support any 
additional archiving above and beyond what NSView does, and you will never use 
it in a nib or storyboard, you can just leave the implementation empty.

> 
> --
> Have a great day,
> Alex Hall
> mehg...@icloud.com
> 
> ___
> 
> 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/david.duncan%40apple.com
> 
> This email sent to david.dun...@apple.com

--
David Duncan

___

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