Sounds like NSValueTransformer is in fact what you need.
It can take in your NSArray ref and spit out a single NSString that the 
NSTextField binding can live with.

Defining NSValueTransformer subclass for every binding can be a pain.
So I use a block approach. This has the big advantage that you can capture 
other variables if required - like calling a weak self method:

    BPBlockValueTransformer * fooTransformer = [BPBlockValueTransformer 
valueTransformerWithBlock:^id(NSArray *a)  {
        return [welf soSomethingWith:a];
    }];
    [self.fooView bind:NSValueBinding toObject:self withKeyPath:@“foo” 
options:@{NSValueTransformerBindingOption: fooTransformer}];


// subclass
@interface BPBlockValueTransformer : NSValueTransformer

+ (instancetype)valueTransformerWithBlock:(id(^)(id value))block;
@property (nonatomic,strong) id (^transform)(id value);

@end

@implementation BPBlockValueTransformer

+ (instancetype)valueTransformerWithBlock:(id(^)(id value))block
{
    BPBlockValueTransformer *transformer = [[self alloc] init];
    transformer.transform = block;
    
    return transformer;
}

+ (Class)transformedValueClass
{
    return [NSObject class];
}

- (id)transformedValue:(id)value
{
    return self.transform(value);
}

@end


> On 6 Mar 2017, at 13:32, Jeremy Hughes <moon.rab...@virginmedia.com> wrote:
> 
>> From what I understand of your example, you’re not “binding” anything in a 
>> Cocoa sense.
> 
> In the case of the single value, the text field is set up via the Bindings 
> pane of Interface Builder so that “Value" says “Bind to File’s Owner” with a 
> model key path of self.value. (And “value" is declared as dynamic so that 
> Swift will take care of KVO.) This works fine. Is this not a Cocoa binding?
> 
> What I don’t understand is why this works for a single value but doesn’t work 
> for an array of values, where I change the model key path to self.values.
> 
>> What you is an NSArrayController. Bind your text field to the array 
>> controller. Supply the array controller with content, and have it derive the 
>> selected value, be it single or multiple.
> 
> OK. I now have an array controller that is bound to File’s Owner with a model 
> key path of self.values, and I then bind the text field to the array 
> controller with a Controller Key value of “selection” (although I’m not sure 
> that’s right, because the array is not actually displayed anywhere for users 
> to select items).
> 
> Now I get the following error:
> 
> Cannot create number from object <_NSControllerObjectProxy: 0x6000000070b0> 
> of class _NSControllerObjectProxy
> 
> Jeremy
> 
> 
> _______________________________________________
> 
> 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/lists%40mugginsoft.com
> 
> This email sent to li...@mugginsoft.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

Reply via email to