MTLModels in MTLModels?

I have a web service that returns the JSON of an object, and inside this object there is a list of other objects. How can I get a mantle to create an object for each of these nested objects, instead of giving me a dictionary for each of them?

+6
source share
1 answer

This can be done using mtl_JSONDictionaryTransformerWithModelClass: tranformer introduced by Mantle some time ago.

Take a look at an example taken from the mantle project file itself :

 @interface GHIssue : MTLModel <MTLJSONSerializing> @property (nonatomic, strong, readonly) GHUser *assignee; @end 
 @implementation GHIssue + (NSDictionary *)JSONKeyPathsByPropertyKey { return @{ @"assignee": @"assignee", }; } + (NSValueTransformer *)assigneeJSONTransformer { return [NSValueTransformer mtl_JSONDictionaryTransformerWithModelClass:[GHUser class]]; } @end 

Assuming GHUser is a subclass of MTLModel that conforms to the MTLJSONSerializing protocol, everything should work fine.

UPDATE: The above solution is now deprecated. The correct way to use it will now be

 return [MTLJSONAdapter dictionaryTransformerWithModelClass:GHUser.class]; 

inside the assigneeJSONTransformer method.

+12
source

Source: https://habr.com/ru/post/949394/


All Articles