0

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]];
Community
  • 1
  • 1
Just a coder
  • 15,480
  • 16
  • 85
  • 138
  • Cocoa-touch doesn't support cocoa bindings. The closest you will get is KVO. See here for more: http://stackoverflow.com/questions/3812972/is-there-any-data-binding-mechanism-available-for-ios – danielbeard Jul 23 '13 at 20:00
  • Or just use associated objects. –  Jul 23 '13 at 20:02
  • I accidentally added Cocoa touch. I have now removed the Cocoa-touch tag. @H2CO3 you say the keypath should be @"associatedObjects" ? – Just a coder Jul 23 '13 at 20:37
  • @H2CO3 How would you emulate bindings with associated objects? I don't really see what these have to do with each other. – omz Jul 23 '13 at 20:39
  • @Jai No. I meant the `objc_[set|get]AssociatedObject()` API. –  Jul 23 '13 at 20:41
  • @omz If I'm not mistaken, OP wants to associate objects with other object quite arbitrarily, but he couldn't find a way for doing so without specifying explicit properties. The question is quite vague TBH, and I apologize if that's not what he wants. –  Jul 23 '13 at 20:43
  • ...Just like someone can bind a cell's stringValue on a NSTableCellView to show the content of myObject.text, i just want to know how (or if its even possible) to bind the cell's stringValue to a NSString. – Just a coder Jul 23 '13 at 20:46
  • 1
    `NSString` is immutable, it doesn't really make sense to bind to an object that never changes... @H2CO3 Read about bindings on OS X, it's a well-defined mechanism that has more to do with KVO than with associated objects (and doesn't exist on iOS). – omz Jul 23 '13 at 20:46
  • @omz Didn't relalize "binding" is the name of a Cocoa technology... Thanks! (And heck, yeah, observing an object which doesn't mutate is quite boring, indeed.) –  Jul 23 '13 at 20:47
  • 1
    The best part about this question is that `stringValue` has never been a valid binding. – CodaFi Jul 23 '13 at 20:48
  • @omz but the NSString value does change. But not by user. It changes with code and constantly updates. So i just wanted the TableViewCell to reflect that. – Just a coder Jul 23 '13 at 20:49
  • 1
    No, the string itself doesn't *change*, it's *replaced* by a different object that knows nothing about the previous value. – omz Jul 23 '13 at 20:50
  • @omz ok last question.. what if the string was NSMutableString ? – Just a coder Jul 23 '13 at 20:51
  • @Jai Then it would suddenly make sense to observe it :) –  Jul 23 '13 at 20:51

1 Answers1

2

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:@{}];
CodaFi
  • 43,043
  • 8
  • 107
  • 153