Using NSValueTransformer with a double

  • Hi-

    I have an object with an instance variable of type NSTimeInterval
    which as you know is actually just a double containing number of
    seconds.

    I would like to bind this ivar to an NSTextField on my interface
    which works fine, except that it displays in seconds of course. I
    want the user to experience this value in minutes.

    So I look at NSValueTransformer which seems to be made exactly for
    this type of thing.

    But to my newb eyes it seems to only want to transform objects (I
    could be wrong).

    Am I wrong, or should I do something like store my NSTimeInterval as
    an NSNumber in my object and redo minimal code to make my other stuff
    work with it?

    I thank you
  • Hi,

    I had exactly the same wish, and no, you don't have to store your
    NSTimeInterval as an object. Interface Builder takes care of it for
    you if you use bindings.

    What you do is this: your value transformer takes an id and returns
    an id. You get the time interval by calling -doubleValue on your
    input argument, you convert that to minutes in any way you want,
    format it into a string, and return that.

    In Interface Builder, you bind the value like you did before, and
    fill in the name of your value transformer in the appropriate field.
    Done! (Of course, you have to register your value transformer first,
    see the documentation).

    Hope this helps,
    Hank

    On Oct 10, 2007, at 10:12 AM, Paul Bruneau wrote:

    > Hi-
    >
    > I have an object with an instance variable of type NSTimeInterval
    > which as you know is actually just a double containing number of
    > seconds.
    >
    > I would like to bind this ivar to an NSTextField on my interface
    > which works fine, except that it displays in seconds of course. I
    > want the user to experience this value in minutes.
    >
    > So I look at NSValueTransformer which seems to be made exactly for
    > this type of thing.
    >
    > But to my newb eyes it seems to only want to transform objects (I
    > could be wrong).
    >
    > Am I wrong, or should I do something like store my NSTimeInterval
    > as an NSNumber in my object and redo minimal code to make my other
    > stuff work with it?
    >
    > I thank you
    >

    Hank Heijink
    <hank.list...>
  • Fabulous, Hank! Thank you. I am actually giddy to try it out!

    On Oct 10, 2007, at 10:32 AM, Hank Heijink wrote:

    > Hi,
    >
    > I had exactly the same wish, and no, you don't have to store your
    > NSTimeInterval as an object. Interface Builder takes care of it for
    > you if you use bindings.
    >
    > What you do is this: your value transformer takes an id and returns
    > an id. You get the time interval by calling -doubleValue on your
    > input argument, you convert that to minutes in any way you want,
    > format it into a string, and return that.
    >
    > In Interface Builder, you bind the value like you did before, and
    > fill in the name of your value transformer in the appropriate
    > field. Done! (Of course, you have to register your value
    > transformer first, see the documentation).
    >
    > Hope this helps,
    > Hank
    >
    > On Oct 10, 2007, at 10:12 AM, Paul Bruneau wrote:
    >
    >> Hi-
    >>
    >> I have an object with an instance variable of type NSTimeInterval
    >> which as you know is actually just a double containing number of
    >> seconds.
    >>
    >> I would like to bind this ivar to an NSTextField on my interface
    >> which works fine, except that it displays in seconds of course. I
    >> want the user to experience this value in minutes.
    >>
    >> So I look at NSValueTransformer which seems to be made exactly for
    >> this type of thing.
    >>
    >> But to my newb eyes it seems to only want to transform objects (I
    >> could be wrong).
    >>
    >> Am I wrong, or should I do something like store my NSTimeInterval
    >> as an NSNumber in my object and redo minimal code to make my other
    >> stuff work with it?
    >>
    >> I thank you
  • On Oct 10, 2007, at 11:32 AM, Stephen F. Booth wrote:

    >> I have an object with an instance variable of type NSTimeInterval
    >> which as you know is actually just a double containing number of
    >> seconds.
    >>
    >> I would like to bind this ivar to an NSTextField on my interface
    >> which works fine, except that it displays in seconds of course. I
    >> want the user to experience this value in minutes.
    >>
    >> So I look at NSValueTransformer which seems to be made exactly for
    >> this type of thing.
    >
    > I would recommend using NSFormatter in lieu of NSValueTransformer
    > for this case, since you are just reformatting data for
    > presentation to the user.  It doesn't seem to me that you actually
    > need to transform the value.  In my experience the lines between
    > NSValueTransformer and NSFormatter can become blurred.

    > I've written an NSFormatter subclass that does what you're after;
    > see http://playosx.svn.sourceforge.net/viewvc/playosx/trunk/
    > Formatters/SecondsFormatter.m?view=markup for the source code.

    OK I'll look at the docs for NSFormatter. Although Apple's example of
    NSValueTransformer where they show a Celcius to Farenheight
    conversion seems to me to be a nearly perfect analogy to my second to
    minute needs. So if my lines are blurry, I blame Apple :)

    Thanks for the link to the source code too!
  • On Oct 10, 2007, at 11:32 AM, Stephen F. Booth wrote:

    >> I have an object with an instance variable of type NSTimeInterval
    >> which as you know is actually just a double containing number of
    >> seconds.
    >>
    >> I would like to bind this ivar to an NSTextField on my interface
    >> which works fine, except that it displays in seconds of course. I
    >> want the user to experience this value in minutes.
    >>
    >> So I look at NSValueTransformer which seems to be made exactly for
    >> this type of thing.
    >
    > I would recommend using NSFormatter in lieu of NSValueTransformer
    > for this case, since you are just reformatting data for
    > presentation to the user.  It doesn't seem to me that you actually
    > need to transform the value.  In my experience the lines between
    > NSValueTransformer and NSFormatter can become blurred.
    >

    OK in looking at NSFormatter I don't think it will work for me
    because although I might have been too subtle in my description, I am
    using Bindings, which NSValueTransformer seems better able to work
    with, and I do want not only to present the transformed value to the
    user, but I need the reversability that NSValueTransformer offers
    because I want the user to be able to adjust the value and have it
    get transformed back into seconds for storage back into my object.

    If I'm not correct, please don't hesitate to redirect me :)
previous month october 2007 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today