Note that there are some explanatory texts on larger screens.

plurals
  1. POCircular dependencies in Cocoa bindings
    primarykey
    data
    text
    <p>This is doing my head in...</p> <p>Simplified version: I have two text fields - Field A and Field B. Field B can be derived from field A and similarly Field B can be derived from Field A.</p> <p>(There's couple of other fields that in combination with either A or B produce data for multiple TextLabels)</p> <p>What I want to do is: when user changes Field A, Field B is updated, and vice versa.</p> <p>So I created two methods that do A to B and B to A. And defined dependencies like:</p> <pre><code>+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key { NSSet *keyPaths = [super keyPathsForValuesAffectingValueForKey:key]; if ([key isEqualToString:@"calculatedFieldA"]) { NSSet *dependentKeys = [NSSet setWithObjects:@"valueOfFieldB", nil]; keyPaths = [keyPaths setByAddingObjectsFromSet:dependentKeys]; } if ([key isEqualToString:@"calculatedFieldB"]) { NSSet *dependentKeys = [NSSet setWithObjects:@"valueOfFieldA", nil]; keyPaths = [keyPaths setByAddingObjectsFromSet:dependentKeys]; } return keyPaths; } </code></pre> <p>Where <code>calculatedFieldA</code> and <code>calculatedFieldB</code> are the methods that do the conversion and <code>valueOfFieldA</code> and <code>valueOfFieldB</code> are NSString's that are bound to the two text fields.</p> <p>It works (but only one way, B is updated whenever A changes) if I remove the second <code>if</code> statement. When the second <code>if</code> is defined, it just bombs out, because (I think) it sees A updated so goes and updates B, then because B is updated, goes and updates A again, etc, etc...</p> <p>What is the best way to achieve this circular dependency? Is it a time to start reading about <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ValueTransformers/ValueTransformers.html#//apple_ref/doc/uid/10000175i" rel="nofollow">ValueTransformers</a>?</p> <p>PS. I'm a Cocoa newbie so please bear with me and don't punch too hard if this is a very trivial question...</p> <p><strong>EDIT:</strong></p> <p>I probably need to clarify few points:</p> <p><code>calculatedFieldA</code> accepts B value and returns A, also updates (via setter method) <code>valueOfFieldA</code>. Similarly <code>calculatedFieldB</code> accepts A value and returns B, also updates (via setter method) <code>valueOfFieldB</code>.</p> <p>This is on Lion, with Xcode 4.1.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload