Re: Validating NSButton in Swift

2015-12-14 Thread dangerwillrobinsondanger
Glad that worked. I'll also file some docs bugs. 

Another way to do this is to implement a controller or container that handles 
it. 

View controllers are in the responder chain and can act as the item that needs 
validation and set the control properties. 
We can also look at the design of toolbar items and menu items as containers 
for views, so they can do a similar job. 

These approaches might let you avoid some control subclassing (depending on 
what else you need to do) and might let you stay in swift. 

Sent from my iPhone

> On Dec 15, 2015, at 6:56 AM, Luc Van Bogaert  wrote:
> 
> 
>>> On 14 Dec 2015, at 22:51, Roland King  wrote:
>>> 
>>> 
 On 15 Dec 2015, at 04:10, Luc Van Bogaert  wrote:
>>> 
>>> 
>>> I tried what you suggested and made a button subclass and just declared 
>>> conformance in objective-c, and then imported the class into Swift.
>>> This seems to do the trick, my button subclass is now accepted as 
>>> conforming to the protocol.
>>> Thanks!
>> 
>> Good - If you can find 5 minutes I really would suggest filing a bug report 
>> on it, it’s nice to be able to work around it for now but it’s something 
>> needs to get fixed, the protocol definition is inconsistent in Objective C 
>> (defined as properties, implemented as functions) and that needs cleaning up 
>> or special-casing. 
> 
> Done it already.
> 
> -- 
> Luc Van Bogaert
___

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: Validating NSButton in Swift

2015-12-14 Thread Luc Van Bogaert

> On 14 Dec 2015, at 22:51, Roland King  wrote:
> 
> 
>> On 15 Dec 2015, at 04:10, Luc Van Bogaert > > wrote:
>>> 
>> 
>> 
>> I tried what you suggested and made a button subclass and just declared 
>> conformance in objective-c, and then imported the class into Swift.
>> This seems to do the trick, my button subclass is now accepted as conforming 
>> to the protocol.
>> Thanks!
>> 
> 
> Good - If you can find 5 minutes I really would suggest filing a bug report 
> on it, it’s nice to be able to work around it for now but it’s something 
> needs to get fixed, the protocol definition is inconsistent in Objective C 
> (defined as properties, implemented as functions) and that needs cleaning up 
> or special-casing. 
> 

Done it already.

-- 
Luc Van Bogaert
___

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: Validating NSButton in Swift

2015-12-14 Thread Roland King

> On 15 Dec 2015, at 04:10, Luc Van Bogaert  wrote:
>> 
> 
> 
> I tried what you suggested and made a button subclass and just declared 
> conformance in objective-c, and then imported the class into Swift.
> This seems to do the trick, my button subclass is now accepted as conforming 
> to the protocol.
> Thanks!
> 

Good - If you can find 5 minutes I really would suggest filing a bug report on 
it, it’s nice to be able to work around it for now but it’s something needs to 
get fixed, the protocol definition is inconsistent in Objective C (defined as 
properties, implemented as functions) and that needs cleaning up or 
special-casing. 

___

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: Validating NSButton in Swift

2015-12-14 Thread Luc Van Bogaert

> On 14 Dec 2015, at 10:27, Roland King  wrote:
> 
> 
>> On 14 Dec 2015, at 17:12, dangerwillrobinsondan...@gmail.com wrote:
>> 
>> The bug should be that it gives you a wrong message, because it already 
>> conforms to the protocol because it inherits from a class that conforms to 
>> the protocol. 
>> 
>> All you should have to do is implement an override of the methods to do 
>> custom stuff or of the properties. 
>> You shouldn't declare conformity to NSValidatedUserInterfaceItem because it 
>> already confirms earlier in its ancestry. 
>> 
> 
> Nope. That’s wrong for 2 reasons. 
> 
> Firstly although NSControl does have the methods on it to conform to the 
> protocol in Objective C, it doesn’t actually declare that it conforms to the 
> protocol anywhere so you don’t get the actual conformance from the bridge 
> over to Swift. Its conformance is informal, not formal. 
> 
> Secondly, the methods on NSControl which implement tag and action are 
> properties in Objective C, not methods. In objective C that doesn’t matter as 
> properties just end up being two methods and the getter method in that case 
> has the right signature for the protocol so, in objective C, you can subclass 
> from any subclass of NSControl, declare protocol conformance at that point, 
> and it works. Swift however imports the properties as properties, not a pair 
> of class functions and properties do not make a class conform to a protocol 
> which requires functions, which the protocol in this instance does. Neither 
> can you override the property with a same-named method, so you can’t do it. 
> 
> I did ask earlier if Luc can declare the conformance in objective-C, 
> literally by making an NSValidatedButton subclass of NSButton which does 
> nothing but formally declare the protocol conformance and then use that in 
> swift. My thinking there is that the formal conformance to the protocol in 
> objc will mean the object is correctly tagged in swift as already conforming. 
> I don’t see another way to do it. 


I tried what you suggested and made a button subclass and just declared 
conformance in objective-c, and then imported the class into Swift.
This seems to do the trick, my button subclass is now accepted as conforming to 
the protocol.
Thanks!

-- 
Luc Van Bogaert


___

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: Validating NSButton in Swift

2015-12-14 Thread dangerwillrobinsondanger
Yeah this was one of those little stumpers. 
The control implements the methods to be eligible for being validated but 
shouldn't validate itself. 
A controller or something else in the responder chain that knows about app 
state should do validation. 

The docs should be bugged. 


Sent from my iPhone

> On Dec 14, 2015, at 6:27 PM, Roland King  wrote:
> 
> 
>> On 14 Dec 2015, at 17:12, dangerwillrobinsondan...@gmail.com wrote:
>> 
>> The bug should be that it gives you a wrong message, because it already 
>> conforms to the protocol because it inherits from a class that conforms to 
>> the protocol. 
>> 
>> All you should have to do is implement an override of the methods to do 
>> custom stuff or of the properties. 
>> You shouldn't declare conformity to NSValidatedUserInterfaceItem because it 
>> already confirms earlier in its ancestry.
> 
> Nope. That’s wrong for 2 reasons. 
> 
> Firstly although NSControl does have the methods on it to conform to the 
> protocol in Objective C, it doesn’t actually declare that it conforms to the 
> protocol anywhere so you don’t get the actual conformance from the bridge 
> over to Swift. Its conformance is informal, not formal. 
> 
> Secondly, the methods on NSControl which implement tag and action are 
> properties in Objective C, not methods. In objective C that doesn’t matter as 
> properties just end up being two methods and the getter method in that case 
> has the right signature for the protocol so, in objective C, you can subclass 
> from any subclass of NSControl, declare protocol conformance at that point, 
> and it works. Swift however imports the properties as properties, not a pair 
> of class functions and properties do not make a class conform to a protocol 
> which requires functions, which the protocol in this instance does. Neither 
> can you override the property with a same-named method, so you can’t do it. 
> 
> I did ask earlier if Luc can declare the conformance in objective-C, 
> literally by making an NSValidatedButton subclass of NSButton which does 
> nothing but formally declare the protocol conformance and then use that in 
> swift. My thinking there is that the formal conformance to the protocol in 
> objc will mean the object is correctly tagged in swift as already conforming. 
> I don’t see another way to do it.

___

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: Validating NSButton in Swift

2015-12-14 Thread dangerwillrobinsondanger
Oh right. 
So this is a clever one. 
The key in the docs might be the responder chain mentions. 
The control doesn't have to validate itself. 
On the contrary, usually some controller validates.
The control generally should know about state elsewhere. 

Sent from my iPhone

> On Dec 14, 2015, at 6:20 PM, Quincey Morris 
>  wrote:
> 
>> On Dec 14, 2015, at 01:12 , dangerwillrobinsondan...@gmail.com wrote:
>> 
>> because it inherits from a class that conforms to the protocol
> 
> NSControl conforms informally (that is, it has the requisite methods) but not 
> formally, which isn’t conformance in Swift terms. But there’s still a 
> conflict between properties and methods, which seems to matter to the Swift 
> compiler, that’s preventing subclasses from adopting conformance.
> 
> If you do this in a playground:
> 
>> import Cocoa
>> extension NSControl : NSValidatedUserInterfaceItem { }
> 
> 
> You get these error messages (plus more for ’tag’):
> 
>> error: type 'NSControl' does not conform to protocol 
>> 'NSValidatedUserInterfaceItem'
>> extension NSControl : NSValidatedUserInterfaceItem
>> 
>> AppKit.NSValidatedUserInterfaceItem:2:17: note: protocol requires function 
>> 'action()' with type '() -> Selector'
>> public func action() -> Selector
>> 
>> AppKit.NSControl:4:16: note: candidate is not a function
>> public var action: Selector { get set }
> 
> That pretty much lays out what the problem is, and as Luc
> 
___

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: Validating NSButton in Swift

2015-12-14 Thread Roland King

> On 14 Dec 2015, at 17:12, dangerwillrobinsondan...@gmail.com wrote:
> 
> The bug should be that it gives you a wrong message, because it already 
> conforms to the protocol because it inherits from a class that conforms to 
> the protocol. 
> 
> All you should have to do is implement an override of the methods to do 
> custom stuff or of the properties. 
> You shouldn't declare conformity to NSValidatedUserInterfaceItem because it 
> already confirms earlier in its ancestry. 
> 

Nope. That’s wrong for 2 reasons. 

Firstly although NSControl does have the methods on it to conform to the 
protocol in Objective C, it doesn’t actually declare that it conforms to the 
protocol anywhere so you don’t get the actual conformance from the bridge over 
to Swift. Its conformance is informal, not formal. 

Secondly, the methods on NSControl which implement tag and action are 
properties in Objective C, not methods. In objective C that doesn’t matter as 
properties just end up being two methods and the getter method in that case has 
the right signature for the protocol so, in objective C, you can subclass from 
any subclass of NSControl, declare protocol conformance at that point, and it 
works. Swift however imports the properties as properties, not a pair of class 
functions and properties do not make a class conform to a protocol which 
requires functions, which the protocol in this instance does. Neither can you 
override the property with a same-named method, so you can’t do it. 

I did ask earlier if Luc can declare the conformance in objective-C, literally 
by making an NSValidatedButton subclass of NSButton which does nothing but 
formally declare the protocol conformance and then use that in swift. My 
thinking there is that the formal conformance to the protocol in objc will mean 
the object is correctly tagged in swift as already conforming. I don’t see 
another way to do it. 
___

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: Validating NSButton in Swift

2015-12-14 Thread Quincey Morris
On Dec 14, 2015, at 01:12 , dangerwillrobinsondan...@gmail.com wrote:
> 
> because it inherits from a class that conforms to the protocol

NSControl conforms informally (that is, it has the requisite methods) but not 
formally, which isn’t conformance in Swift terms. But there’s still a conflict 
between properties and methods, which seems to matter to the Swift compiler, 
that’s preventing subclasses from adopting conformance.

If you do this in a playground:

> import Cocoa
> extension NSControl : NSValidatedUserInterfaceItem { }


You get these error messages (plus more for ’tag’):

> error: type 'NSControl' does not conform to protocol 
> 'NSValidatedUserInterfaceItem'
> extension NSControl : NSValidatedUserInterfaceItem
> 
> AppKit.NSValidatedUserInterfaceItem:2:17: note: protocol requires function 
> 'action()' with type '() -> Selector'
> public func action() -> Selector
> 
> AppKit.NSControl:4:16: note: candidate is not a function
> public var action: Selector { get set }

That pretty much lays out what the problem is, and as Luc

___

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: Validating NSButton in Swift

2015-12-14 Thread dangerwillrobinsondanger
The bug should be that it gives you a wrong message, because it already 
conforms to the protocol because it inherits from a class that conforms to the 
protocol. 

All you should have to do is implement an override of the methods to do custom 
stuff or of the properties. 
You shouldn't declare conformity to NSValidatedUserInterfaceItem because it 
already confirms earlier in its ancestry. 



Sent from my iPhone

> On Dec 14, 2015, at 6:02 PM, Quincey Morris 
>  wrote:
> 
>> On Dec 13, 2015, at 23:53 , Roland King  wrote:
>> 
>> You appear to be a little out of luck here because
> 
> I was afraid that was going to be the answer. I suspect the problem is the 
> Swift Obj-C importer, rather than any misguided SDK intention, because there 
> are also a *lot* of accessibility properties that come across as functions. 
> Maybe I’ll file a bug report for that manifestation too.
> 
> ___
> 
> 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/dangerwillrobinsondanger%40gmail.com
> 
> This email sent to dangerwillrobinsondan...@gmail.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/archive%40mail-archive.com

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

Re: Validating NSButton in Swift

2015-12-14 Thread Quincey Morris
On Dec 13, 2015, at 23:53 , Roland King  wrote:
> 
> You appear to be a little out of luck here because 

I was afraid that was going to be the answer. I suspect the problem is the 
Swift Obj-C importer, rather than any misguided SDK intention, because there 
are also a *lot* of accessibility properties that come across as functions. 
Maybe I’ll file a bug report for that manifestation too.

___

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: Validating NSButton in Swift

2015-12-13 Thread Roland King

> On 14 Dec 2015, at 15:42, Luc Van Bogaert  wrote:
> 
> class ValidatedButton: NSButton, ValidatedControl {
> 
> 
> I've defined ValidatedControl like this:
> 
> protocol ValidatedControl: NSValidatedUserInterfaceItem {
> 
> 
> This results in a compiler error: Type ValidatedButton does not conform to 
> protocol NSValidatedUserInterfaceItem.
> I've tried overriding the NSControl properties 'action' and 'tag', but the 
> error remains. 

You appear to be a little out of luck here because 

NSValidatedUserInterfaceItem requires two *functions*

public func tag()->Int
public func action()->Selector

however NSControl implements two *properties*

public var tag : Int { get set }
public var action : Selector { get set }

and even though the getter for those has the same signature you can’t 
substitute one for another. Nor can you add a function on the subclass which 
shadows the equivalently-named property on the superclass so .. it rather looks 
like you can’t do it. 

Can you declare this small piece of it in ObjC and import the classes, while 
Apple considers the bug report you will no-doubt file on it. 
___

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: Validating NSButton in Swift

2015-12-13 Thread Luc Van Bogaert
> On 14 Dec 2015, at 00:18, Quincey Morris 
>  wrote:
> 
> On Dec 13, 2015, at 14:59 , Luc Van Bogaert  > wrote:
>> 
>> I have a NSButton subclass that I want to conform to 
>> NSValidatedUserInterfaceItem, so I can call validateUserInterfaceItem: on a 
>> validator passing the button as a parameter. This requires my subclass to 
>> implement two methods: action() and tag(). The compiler complains about the 
>> selectors being identical to the 'action' and 'tag' selectors in superclass 
>> NSControl.
> 
> What do your subclass implementations look like (the signature, not the body)?
> 
> According to the documentation, NSControl already has the conforming 
> properties, so you don’t need to define your own. Just assign the correct 
> values to the existing properties.
> 
> However, the Swift version of NSValidatedUserInterfaceItem declares the 
> properties as methods, so this may be confusing the issue. In Obj-C, it’s all 
> the same thing — the getter for “action” is also the method “action”, but I’m 
> not sure what happens during bridging. (It ought to realize they’re the same 
> thing.)
> 
> What happens if you *don’t* declare action and tag in your subclass? Is there 
> a non-conformance warning?



I've defined the subclass like this: 

class ValidatedButton: NSButton, ValidatedControl {


I've defined ValidatedControl like this:

protocol ValidatedControl: NSValidatedUserInterfaceItem {


This results in a compiler error: Type ValidatedButton does not conform to 
protocol NSValidatedUserInterfaceItem.
I've tried overriding the NSControl properties 'action' and 'tag', but the 
error remains. 

-- 
Luc
___

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: Validating NSButton in Swift

2015-12-13 Thread Quincey Morris
On Dec 13, 2015, at 14:59 , Luc Van Bogaert  wrote:
> 
> I have a NSButton subclass that I want to conform to 
> NSValidatedUserInterfaceItem, so I can call validateUserInterfaceItem: on a 
> validator passing the button as a parameter. This requires my subclass to 
> implement two methods: action() and tag(). The compiler complains about the 
> selectors being identical to the 'action' and 'tag' selectors in superclass 
> NSControl.

What do your subclass implementations look like (the signature, not the body)?

According to the documentation, NSControl already has the conforming 
properties, so you don’t need to define your own. Just assign the correct 
values to the existing properties.

However, the Swift version of NSValidatedUserInterfaceItem declares the 
properties as methods, so this may be confusing the issue. In Obj-C, it’s all 
the same thing — the getter for “action” is also the method “action”, but I’m 
not sure what happens during bridging. (It ought to realize they’re the same 
thing.)

What happens if you *don’t* declare action and tag in your subclass? Is there a 
non-conformance warning?
___

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