• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Aggravation trying to implement NSValueTransformer subclasses in Swift
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Aggravation trying to implement NSValueTransformer subclasses in Swift


  • Subject: Re: Aggravation trying to implement NSValueTransformer subclasses in Swift
  • From: William Squires <email@hidden>
  • Date: Thu, 14 May 2015 10:40:49 -0500

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 <email@hidden> wrote:

> On May 12, 2015, at 14:29 , William Squires <email@hidden> wrote:
>>
>> class IsNotEmptyTransformer : NSValueTransformer
>> {
>> }
>>
>> but the example in the documentation is in ObjC, not Swift, and refers to id, not to "Bool"s or "String"s. 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 (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


  • Follow-Ups:
    • Re: Aggravation trying to implement NSValueTransformer subclasses in Swift
      • From: Quincey Morris <email@hidden>
References: 
 >Aggravation trying to implement NSValueTransformer subclasses in Swift (From: William Squires <email@hidden>)
 >Re: Aggravation trying to implement NSValueTransformer subclasses in Swift (From: Quincey Morris <email@hidden>)

  • Prev by Date: Re: How to draw a NSView on top of a WebView and don't get overriden trying
  • Next by Date: Fast enumeration question.
  • Previous by thread: Re: Aggravation trying to implement NSValueTransformer subclasses in Swift
  • Next by thread: Re: Aggravation trying to implement NSValueTransformer subclasses in Swift
  • Index(es):
    • Date
    • Thread