0

I am using the Mantle framework in iOS for a simple JSON structure that looks like this:

{
    "posts":
    [
        {
            "postId": "123",
            "title": "Travel plans",
            "location": "Europe"
        },
        {
            "postId": "456",
            "title": "Vacation Photos",
            "location": "Asia"
        }
    ],
    "updates": [
        {
            "friendId": "ABC123"
        }
    ]
}

Essentially I am only interested in the "posts" key and wish to completely ignore the "updates" key. Additionally within the "posts" array I wish to completely ignore the "location" key. Here is how I set up my Mantle Models:

@interface MantlePost: MTLModel <MTLJSONSerializing>

@property (nonatomic, strong) NSString *postId;
@property (nonatomic, strong) NSString *title;

@end


@implementation MantlePost


+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return @{
             @"postId": @"postId",
             @"title": @"title",
             };
}

@end

And here is my MantlePosts model:

@interface MantlePosts: MTLModel<MTLJSONSerializing>
@property (nonatomic, strong) NSArray<MantlePost *> *posts;

@end

@implementation MantlePosts

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return @{
             @"posts": @"posts"
             };
}

+ (NSValueTransformer *)listOfPosts {
    return [MTLJSONAdapter arrayTransformerWithModelClass:MantlePost.class];
}

@end

Finally, here is how I load my JSON up to be converted:

- (NSDictionary *)loadJSONFromFile {

    NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"parse-response" ofType:@"json"];
    NSError *error = nil;

    NSData *jsonData = [[NSString stringWithContentsOfFile:jsonPath usedEncoding:nil error:&error] dataUsingEncoding:NSUTF8StringEncoding];

    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:&error];

    return jsonDict;
}


NSError = nil;

NSDictionary *jsonData = [self loadJSONFromFile];

MantlePosts *posts = (MantlePosts *)[MTLJSONAdapter modelOfClass:MantlePosts.class fromJSONDictionary:jsonData error:&error];

The problem is, my descendent array of MantlePosts contains all 3 properties postId, title, and location, when I explicitly mapped only postId and title. The "updates" array is ignored which is what I wanted but I've been stuck being able to ignore certain keys in the descendent arrays. Any help on this would be appreciated.

Here is an example of what I receive when i po the response in the console.

(lldb) po posts
<MantlePosts: 0x6000000153c0> {
    posts =     (
                {
            location = Europe;
            postId = 123;
            title = "Travel plans";
        },
                {
            location = Asia;
            postId = 456;
            title = "Vacation Photos";
        }
    );
}

(lldb)

enter image description here

zic10
  • 2,310
  • 5
  • 30
  • 55
  • What do you mean by "MantlePosts contains all 3 properties"? You have declared just 2 properties, how can you access the 3rd one? – battlmonstr Jun 10 '18 at 14:45
  • That's what I don't understand. All properties of the JSON were being mapped even though I only specified 2 properties on the MantlePost – zic10 Jun 10 '18 at 17:44
  • How do you know they are being "mapped"? Do you have any example code that shows that? – battlmonstr Jun 10 '18 at 19:32
  • I've gone ahead and updated the post showing my response in the console as well as how I'm loading my JSON file. – zic10 Jun 10 '18 at 20:10

0 Answers0