Question

NSValueTransformer is used to transform a value. But what if the transformation needs more contextual information? For example how can a NSValueTransformer access other objects in the nib, or access the view controller? Is this just not what NSValueTransformer is meant to be used?

As an example I want to bind a text view's attributed string to an employee's name, using a value transformer to format the name based on the employee's department.

My solution so far has been to just implement a new employeeNameForDisplay property or so, skipping value transformers altogether.

If that's truly the best solution, I have a hard time understand the value of value transformers. Are they really this limited?

Était-ce utile?

La solution

Doc quote ...

NSValueTransformer is an abstract class that is used by the Cocoa Bindings technology to transform values from one representation to another.

... it's a very simple class, which transforms values between different representations. Sometimes I do use it with Cocoa Bindings, but I heavily do use it with CoreData where I need better (smaller, faster, ...) representation for underlying sqlite store. So, this class has the value for us even if you can't achieve your goal with it.

Also I would solve like you do. New property along with ...

+ (NSSet *)keyPathsForValuesAffectingEmployeeNameForDisplay:(NSString *)key {
    return [NSSet setWithArray:@[ @"employeeName", @"employeeDepartment" ]];
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top