Question

I thought this question would work for my situation, but my lack of experience with Mantle and iOS in general has dead ended my train of thought. Basically, I have a big chunk of JSON with nested dictionaries and arrays that I want to convert into Mantle objects.

"features": {
    "App": {
        "status": "_ACTIVE",
        "unavailableReasons": [],
        "modernCapabilities": [{
            "capabilityType": "LOCK_AUTO_REPLY",
            "providerStatuses": [{
                "providerType": "MY_PROVIDER",
                "status": false,
                "unavailableReasons": ["NOT_SUPPORTED_BY_PRODUCT", "DEVICE_OS_NOT_SUPPORTED"]
            }]
        }, 
         ...
         ...             
        {
            "capabilityType": "LOCK_CONTACT_WHITELIST",
            "providerStatuses": [{
                "providerType": "OTHER_PROVIDER",
                "status": true,
                "unavailableReasons": []
            }]
        }
        ]}
 }

I want to be able to implement a similar solution to that linked above, namely iterating down the dictionary for the "Features" key, and applying a transform to each key/value pair. So, in this case, to the "App" key and it's dictionary value (and later, the "modernCapabilities" key and it's array, etc. etc.) I know that for the later steps I'll need separate model classes, and those exist, but I'm having trouble with the first step, the transform on the "App" key and it's value.

Here's what I have right now:

+ (NSValueTransformer *)featureTypesJSONTransformer {
    NSValueTransformer *transformer = [NSValueTransformer valueTransformerForName:@"FeatureStatus"];

    return [MTLValueTransformer transformerWithBlock:^NSDictionary *(NSDictionary *features) {
        NSMutableDictionary *transformedValues = [[NSMutableDictionary alloc] init];
        for (NSString *key in features) {
            id transformedValue = [transformer transformedValue:[features objectForKey:key]];
            if (transformedValue ) {
                [transformedValues setObject:transformedValue forKey:key];
            }
        }

        return transformedValues;
    }];
}

As you can see from the code, I'm trying to preserve the key, and attach it to a new Dictionary with the value being another transform, FeatureStatus in this case (@{ "App" : })

The problem is that there is no [FeatureStatus transformedValue:], although I do have a JSONKeyPathsForPropetyKey since I want to map the 'status', 'unavailableReasons' and 'modernCapabilities' keys later on.

What's my next step? How can I register a ValueTransform that does what I want it to do?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top