Search code examples
objective-ccocoacocoa-bindings

How do i bind to a NSString?


This question was asked before, but its not answered.

I can bind to objects like so:

[[aCellView textField] bind:@"stringValue"
                               toObject:myObject
                            withKeyPath:@"text"
                                options:nil]];

This works easy because i have a myObject.text value. But how do i bind to NSString?

[[aCellView textField] bind:@"stringValue"
                               toObject:aString
                            withKeyPath:@""  // What should the keyPath be?
                                options:nil]];

Solution

  • You can bind to individual objects, but not temporaries. One of the reasons why we bind to key paths is to have a little more sense about the lifetime of the variable, and therefore, the binding. Make aString a property of the calling object, and bind to self with aString as the key path:

    [[aCellView textField] bind:NSValueBinding toObject:self withKeyPath:@"aString" options:@{}];