Manual Cocoa Binding - Custom Value Transformer

  • When manually binding a control how can I also bind my custom value
    transformer?

    I've tried:

    NSMutableDictionary *bindingOptions = [NSMutableDictionary dictionary];
    [bindingOptions setObject:@"MyCustomValueTransformer"
    forKey:@"NSValueTransformerName"];
    [myControl  bind:@"value"  toObject:arrayController
    withKeyPath:@"selection.optionValue"  options:bindingOptions];

    and also:

    MyCustomValueTransformer *transformer = [[MyCustomValueTransformer
    alloc] init];

    NSMutableDictionary *bindingOptions = [NSMutableDictionary dictionary];
    [bindingOptions setObject: transformer
    forKey:@"NSValueTransformerName"];
    [myControl  bind:@"value"  toObject:arrayController
    withKeyPath:@"selection.optionValue"  options:bindingOptions];

    The error returned for either of these examples is: Cannot find value
    transformer with name MyCustomValueTransformer

    Has anyone manually bound a control with a custom value transformer and
    if so what do I need to do?

    Thanks in advance,

    Jonathan
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • On Jul 16, 2004, at 11:09 PM, Jonathan Younger wrote:

    > When manually binding a control how can I also bind my custom value
    > transformer?
    >
    <http://homepage.mac.com/mmalc/CocoaExamples/controllers.html>

    > The error returned for either of these examples is: Cannot find value
    > transformer with name MyCustomValueTransformer

    Have you registered it?

    <http://developer.apple.com/documentation/Cocoa/Conceptual/
    ValueTransformers/Concepts/Registration.html
    >

    mmalc
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • On Jul 16, 2004, at 11:09 PM, Jonathan Younger wrote:
    > NSMutableDictionary *bindingOptions = [NSMutableDictionary dictionary];
    > [bindingOptions setObject: transformer
    > forKey:@"NSValueTransformerName"];
    > [myControl  bind:@"value"  toObject:arrayController
    > withKeyPath:@"selection.optionValue"  options:bindingOptions];
    >
    > The error returned for either of these examples is: Cannot find value
    > transformer with name MyCustomValueTransformer

    There are two solutions:

    (1) register your value transformer via...

    + (void)setValueTransformer:(NSValueTransformer *)transformer
    forName:(NSString *)name;

    ... on NSValueTransformer....

    [NSValueTransformer setValueTransformer: [[[MyCustomValueTransformer
    alloc] init] autorelease] forName: @"MyCustomValueTransformer"];

    ... then  pass the name for the NSValueTransformerName option...

    NSMutableDictionary *bindingOptions = [NSMutableDictionary dictionary];
    [bindingOptions setObject: @"MyCustomValueTransformer"
    forKey:@"NSValueTransformerName"];
    [myControl  bind:@"value"  toObject:arrayController
    withKeyPath:@"selection.optionValue"  options:bindingOptions];

    (2) Bind with a specific instance of NSValueTransformer...

    MyCustomValueTransformer *valueTransformer =
    [[[MyCustomValueTransformer alloc] init] autorelease];
    NSMutableDictionary *bindingOptions = [NSMutableDictionary dictionary];
    [bindingOptions setObject: valueTransformer
    forKey:@"NSValueTransformer"];
    [myControl  bind:@"value"  toObject:arrayController
    withKeyPath:@"selection.optionValue"  options:bindingOptions];
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • > (2) Bind with a specific instance of NSValueTransformer...
    >
    > MyCustomValueTransformer *valueTransformer =
    > [[[MyCustomValueTransformer alloc] init] autorelease];
    > NSMutableDictionary *bindingOptions = [NSMutableDictionary dictionary];
    > [bindingOptions setObject: valueTransformer
    > forKey:@"NSValueTransformer"];
    >
    > [myControl  bind:@"value"  toObject:arrayController
    > withKeyPath:@"selection.optionValue"  options:bindingOptions];

    I'm looking for it, but I can't see how this differ's from Jonathan's
    second attempt. As an aside, I thought that -dictionary returns an
    immutable dictionary even when sent to NSMutableDictionary? Then
    again, the error "attempt to modify immutable" doesn't happen, and
    I've heard a rumour that some "immutable" instances are really
    mutable....

    --

    Brent Gulanowski
    http://www.boredastronaut.com
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.
  • On Jul 18, 2004, at 11:09 AM, Brent Gulanowski wrote:
    > I'm looking for it, but I can't see how this differ's from Jonathan's
    > second attempt. As an aside, I thought that -dictionary returns an
    > immutable dictionary even when sent to NSMutableDictionary? Then
    > again, the error "attempt to modify immutable" doesn't happen, and
    > I've heard a rumour that some "immutable" instances are really
    > mutable....

    The option name is "NSValueTransformer", not "NSValueTransformerName"
    as it was in the original version.

    +dictionary returns a mutable dictionary when invoked upon
    NSMutableDictionary.

    Rumors are generally easily dispelled by reading the documentation.
    Programming by rumor is generally counterproductive.

    b.bum
    _______________________________________________
    cocoa-dev mailing list | <cocoa-dev...>
    Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
    Do not post admin requests to the list. They will be ignored.