Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Adding fixed array index property mapping #21

Merged
merged 3 commits into from
Jul 9, 2015

Conversation

fikeminkel
Copy link
Contributor

I've encountered a few different scenarios where it's clumsy to create object classes as wrappers for elements in a JSON array when I'm only going to use one or two properties from a specific item in that array. I thought it'd be great if you could reference the array index directly in the keyPath string.

With this pull request you can use an array index as part of the keyPath string. Given a structure like

{"obj": [{
  "someKey": "someValue"
},{
  "someKey": "anotherValue"
}]}

you could now get someValue into a property on your root object like this:

@interface SomeObject : NSObject

@property (nonatomic, strong) NSString *myval;

@end

@implementation SomeObject

+ (NSDictionary *)mts_mapping
{
  return @{@"obj.0.someKey": mts_key(myval)};
}
@end

Previously you'd have to create a new object and use that with mts_arrayClassMapping along with some additional code to pull the value into SomeObject.myval.

fikeminkel and others added 3 commits July 8, 2015 14:40
vilanovi added a commit that referenced this pull request Jul 9, 2015
Adding fixed array index property mapping
@vilanovi vilanovi merged commit 3a30645 into mobilejazz:master Jul 9, 2015
@vilanovi
Copy link
Contributor

vilanovi commented Jul 9, 2015

Hello @fikeminkel,

I just published your changes in the new 1.3.0 version, already available in Cocoa Pods.

However I've added/changed:

  • Fixing a crash when defining an array index in the motis mapping that is out of bounds. For example, if you define "avatars.3.image_url", but the avatars array has only 3 objects (or less), Motis is not launching any exception (out of bounds exception, for doing an [array objectAtIndex:3]).
  • Adding tests.

Thanks for your pull request!

@fikeminkel
Copy link
Contributor Author

Awesome. Thanks for accepting this so quickly!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants