Re: Aggravation trying to implement NSValueTransformer subclasses in Swift

2015-05-14 Thread Quincey Morris
On May 14, 2015, at 08:40 , William Squires wsqui...@satx.rr.com wrote:
 
 I'd like to know how to properly write a value transformer in Swift.

Something like this, I expect:

 class StringNotNilTransformer: NSObject {
   
   static var transformedValueClass: AnyClass { return NSNumber.self }
   static let allowsReverseTransformation = false
   
   func transformedValue (value: AnyObject?) - AnyObject? {
   if let string = value as? String {
   return NSNumber (bool: string != );
   }
   return NSNumber (bool: false);
   }
 }


I only checked this in a playground, but I suspect the following slightly 
simpler version would also work too:

 class StringNotNilTransformer: NSObject {
   
   static var transformedValueClass: AnyClass { return NSNumber.self }
   static let allowsReverseTransformation = false
   
   func transformedValue (value: AnyObject?) - AnyObject? {
   if let string = value as? String {
   return string != ;
   }
   return false;
   }
 }



___

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: Aggravation trying to implement NSValueTransformer subclasses in Swift

2015-05-14 Thread William Squires
Thanks, I didn't even think of that, but yeah, it would be easier. Still, I'd 
like to know how to properly write a value transformer in Swift.

On May 12, 2015, at 5:38 PM, Quincey Morris 
quinceymor...@rivergatesoftware.com wrote:

 On May 12, 2015, at 14:29 , William Squires wsqui...@satx.rr.com wrote:
 
 class IsNotEmptyTransformer : NSValueTransformer
 {
 }
 
 but the example in the documentation is in ObjC, not Swift, and refers to 
 id, not to Bools or Strings. Hints, anyone?
 
 Using a value transformer at all seems like a poor choice, and using one in 
 Swift seems even less desirable.
 
 If you are intent on using one, you’ll need to ask a more specific question. 
 What ‘id’ are you referring to? If you’re talking about the transformed 
 value, then you have to use an object — specifically NSNumber to represent a 
 boolean value. IOW, for your use case, the transformer would transform 
 between NSNumber and NSString.
 
 Surely it would be far easier, though, to do what you would do in a modern 
 Obj-C app: use a derived property. Add a new property to the window 
 controller:
 
 class MyWindowController : NSWindowController
 {
  dynamic var message: String
  dynamic var messageIsEmpty: Bool {return String == “”}
 
 and bind the button’s Enabled binding to the “messageIsEmpty” property. 
 That’s not quite all, though, because as it stands, “messageIsEmpty” isn’t 
 KVO-compliant, so you also need to add:
 
  static var keyPathsForValuesAffectingMessageIsEmpty: NSSet {return NSSet 
 (object: messageIsEmpty”)}
 
 (All code written in Mail, not tested.)
 

___

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: Aggravation trying to implement NSValueTransformer subclasses in Swift

2015-05-13 Thread Quincey Morris
On May 12, 2015, at 22:19 , Uli Kusterer witness.of.teacht...@gmx.net wrote:
 
 I think at least one of those should be “message” instead of “messageIsEmpty” 
 … ?

Indeed — the second one.

I’m also not absolutely sure that a computed static property will have the 
desired effect. It may have to be a static method. But perhaps they’re the same 
thing from the Obj-C point of view.

___

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

Aggravation trying to implement NSValueTransformer subclasses in Swift

2015-05-12 Thread William Squires
I'm trying to make what should be a fairly simple value transformer. I need to 
bind the Enabled state of a control (NSButton in this case, but it could be any 
control) to the .isEmpty state of a string. I would think this a common enough 
task that an existing value transformer would be supplied, but I guess not. 
Unfortunately, the documentation only gives a sample in ObjC, but not Swift.

I have the following:

class MyWindowController : NSWindowController
{
  dynamic var message: String

  override var windowNibName: String
{
return MyWindowController
}
  ...
}

there's an NSTextField in the window, and it's value is bound to the message 
variable with bindings; simple enough. There's also a button whose enabled 
status I want controlled based on if the value of message is the empty 
string, so I make:

class IsNotEmptyTransformer : NSValueTransformer
{
}

but the example in the documentation is in ObjC, not Swift, and refers to id, 
not to Bools or Strings. Hints, anyone?


___

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: Aggravation trying to implement NSValueTransformer subclasses in Swift

2015-05-12 Thread Jens Alfke

 On May 12, 2015, at 2:29 PM, William Squires wsqui...@satx.rr.com wrote:
 
 but the example in the documentation is in ObjC, not Swift, and refers to id, 
 not to Bools or Strings. Hints, anyone?

‘id’ in Obj-C is ‘AnyObject’ in Swift. You’ll need to use the “as” or “as?” 
operator to type-cast it to NSNumber or NSString. You should also read Apple’s 
book on using Swift with Objective-C, because there’s a lot more to know before 
you get started.

—Jens
___

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: Aggravation trying to implement NSValueTransformer subclasses in Swift

2015-05-12 Thread Quincey Morris
On May 12, 2015, at 15:38 , Quincey Morris 
quinceymor...@rivergatesoftware.com wrote:
 
 dynamic var message: String
 dynamic var messageIsEmpty: Bool {return String == “”}
 static var keyPathsForValuesAffectingMessageIsEmpty: NSSet {return NSSet 
 (object: messageIsEmpty”)}

FWIW, a Swift-ier way to do this might be something like:

dynamic var message: String =  {
didSet {messageIsEmpty = message == }
}
private(set) dynamic var messageIsEmpty: Bool = true

This is at the cost of actually storing the derived property value, though 
that’s not likely to be a problem in window controller.

(Again, not tested.)



___

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: Aggravation trying to implement NSValueTransformer subclasses in Swift

2015-05-12 Thread Quincey Morris
On May 12, 2015, at 14:29 , William Squires wsqui...@satx.rr.com wrote:
 
 class IsNotEmptyTransformer : NSValueTransformer
 {
 }
 
 but the example in the documentation is in ObjC, not Swift, and refers to id, 
 not to Bools or Strings. Hints, anyone?

Using a value transformer at all seems like a poor choice, and using one in 
Swift seems even less desirable.

If you are intent on using one, you’ll need to ask a more specific question. 
What ‘id’ are you referring to? If you’re talking about the transformed value, 
then you have to use an object — specifically NSNumber to represent a boolean 
value. IOW, for your use case, the transformer would transform between NSNumber 
and NSString.

Surely it would be far easier, though, to do what you would do in a modern 
Obj-C app: use a derived property. Add a new property to the window controller:

class MyWindowController : NSWindowController
{
 dynamic var message: String
 dynamic var messageIsEmpty: Bool {return String == “”}

and bind the button’s Enabled binding to the “messageIsEmpty” property. That’s 
not quite all, though, because as it stands, “messageIsEmpty” isn’t 
KVO-compliant, so you also need to add:

 static var keyPathsForValuesAffectingMessageIsEmpty: NSSet {return NSSet 
(object: messageIsEmpty”)}

(All code written in Mail, not tested.)



___

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: Aggravation trying to implement NSValueTransformer subclasses in Swift

2015-05-12 Thread Uli Kusterer
On 13 May 2015, at 00:38, Quincey Morris quinceymor...@rivergatesoftware.com 
wrote:
 static var keyPathsForValuesAffectingMessageIsEmpty: NSSet {return NSSet 
 (object: messageIsEmpty”)}

I think at least one of those should be “message” instead of “messageIsEmpty” … 
?

Cheers,
-- Uli Kusterer
“The Witnesses of TeachText are everywhere...”
http://zathras.de


___

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