Binding programmatically with key paths


Hi everyone, I am having trouble making binds programmatically.

I am extending the fido slider exercise proposed in this chapter.
Everytime setFido is called, I am creating a Person object, setting its age property to the value of fido and adding it to a muttable array defined in AppController.

What I am trying to make happen is while you slide the slider, a textField will output the average age of all objects in the array.

More code less words :wink:

#import <Cocoa/Cocoa.h>
#import “Person.h”

@interface AppController : NSObject {
int fido;
NSMutableArray *fidos;
IBOutlet NSTextField *fidoAvg;

  • (int)fido;
  • (void)setFido:(int)x;

#import “AppController.h”

@implementation AppController

  • (id)init {
    [super init];
    fidos = [[NSMutableArray alloc] init];
    [fidoAvg bind:@“value” toObject:self withKeyPath:@“fidos.@avg.age” options:nil];
    [self setValue:[NSNumber numberWithInt:5] forKey:@“fido”];
    return self;
    //@synthesize fido;
  • (int)fido {
    NSLog(@"-fido is returning %d", fido);
    return fido;
  • (void)setFido:(int)x {
    NSLog(@"-setFido: is called with %d", x);
    fido = x;
    Person *p = [[Person alloc] init];
    [p setAge:fido];
    [fidos addObject:p];

The binding is in bold. Evidently I am doing something wrong because its not working. Can you throw some light upon me?!
Thanks in advance.