iosobjective-cnsvaluetransformer

NSValueTransformer, how to use?


I am using NSValueTransformer class to transform a NSDictionary object to custom object.

I have already implemented the subclass with the methods:

+ (Class)transformedValueClass
+ (BOOL)allowsReverseTransformation
- (id)transformedValue:(id)value

Then I have registered my subclass with:

[NSValueTransformer setValueTransformer:[[ClassNameTransformer alloc] init] forName:ClassNameTransformerName];

How I am supposed now to get my custom object from a NSDictionary. Since I have not found any info about this, only when used with bindings like in CoreData and .xib files.

Can I use NSValueTransformer for my purpose? -> Transform NSDictionary into my custom Object.

Thanks!


Solution

  • Of course you can use NSValueTransformer stand-alone

    ClassNameTransformer *transformer = [[ClassNameTransformer alloc] init];
    MyObject *object = (MyObject *)[transformer transformedValue:dictionary];