Skip to content

Commit 7c00eff

Browse files
author
Josh Holtz
committed
Merge branch 'master' of github.com:joshdholtz/jsonapi-ios
2 parents 690525f + 642695d commit 7c00eff

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

README.md

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Clone the repository and drop in the .h and .m files from the "Classes" director
2828
JSONAPI is available through [CocoaPods](http://cocoapods.org), to install
2929
it simply add the following line to your Podfile:
3030

31-
pod 'JSONAPI', '~> 0.1.1'
31+
pod 'JSONAPI', '~> 0.1.2'
3232

3333
## Usage
3434

@@ -41,6 +41,8 @@ it simply add the following line to your Podfile:
4141
#### Resource mappings
4242
`(NSDictionary*)mapKeysToProperties` can be overwritten to define a dictionary mapping of JSON keys to map into properties of a subclassed JSONAPIResource. Use a "links." prefix on the JSON key to map a linked JSONAPIResource model or array of JSONAPIResource models
4343

44+
##### Usage
45+
4446
````objc
4547

4648
@implementation ASubclassedResource
@@ -59,6 +61,34 @@ it simply add the following line to your Podfile:
5961

6062
````
6163
64+
##### Map values outside of `mapKeysToProperties` method
65+
If you need to map values that are a little odd, like mapping to enums or performing some sort of formatting before setting a property, you can override the `initWithDictionary` method and assign properties in there.
66+
67+
````objc
68+
69+
typedef enum {
70+
JESSE,
71+
CHESTER
72+
} Character;
73+
74+
@property (nonatomic, assign) Character character;
75+
76+
- (id)initWithDictionary:(NSDictionary *)dict withLinked:(NSDictionary *)linked {
77+
self = [super initWithDictionary:dict withLinked:linked];
78+
if (self) {
79+
// Do stuff in there
80+
NSString *tatoo = [self objectForKey:@"tatoo"];
81+
if ([someKey isEqualToString:@"dude"]) {
82+
character = JESSE;
83+
} else if ([someKey isEqualToString:@"sweet"]) { {
84+
character = CHESTER
85+
}
86+
}
87+
return self;
88+
}
89+
90+
````
91+
6292
### JSONAPIResourceLinker
6393
`JSONAPIResourceLinker` is used for configuring the type of 'links' resources to 'linked' resources.
6494

@@ -146,7 +176,7 @@ NSString *json = @"{\"posts\":[{\"id\":1,\"name\":\"A post!\",\"links\":{\"autho
146176
// Links "author" resource to "people" linked resources
147177
[JSONAPIResourceLinker link:@"author" toLinkedType:@"people"];
148178

149-
//
179+
// Loads "people" into `PeopleResource` and "posts" into `PostResource`
150180
[JSONAPIResourceModeler useResource:[PeopleResource class] toLinkedType:@"people"];
151181
[JSONAPIResourceModeler useResource:[PostResource class] toLinkedType:@"posts"];
152182

@@ -224,7 +254,7 @@ NSString *json = @"{\"posts\":[{\"id\":1,\"name\":\"A post!\",\"links\":{\"autho
224254
// Links "author" resource to "people" linked resources
225255
[JSONAPIResourceLinker link:@"author" toLinkedType:@"people"];
226256
227-
//
257+
// Loads "people" into `PeopleResource`, "posts" into `PostResource`, and "comments" into `CommentResource`
228258
[JSONAPIResourceModeler useResource:[PeopleResource class] toLinkedType:@"people"];
229259
[JSONAPIResourceModeler useResource:[PostResource class] toLinkedType:@"posts"];
230260
[JSONAPIResourceModeler useResource:[CommentResource class] toLinkedType:@"comments"];

0 commit comments

Comments
 (0)