Skip to content

Commit 76d55f2

Browse files
author
Josh Holtz
committed
Update README.md
1 parent e61bbdc commit 76d55f2

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

README.md

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,25 @@ it simply add the following line to your Podfile:
4646
#### Formatter
4747
`JSONAPIResourceFormatter` is used to format values before getting mapped from `mapKeysToProperties`.
4848

49+
Below is an example to register a "Date" function to format a date in a NSString object to an NSDate object before its mapped to the JSONAPIResource instance.
4950

51+
````objc
52+
53+
[JSONAPIResourceFormatter registerFormat:@"Date" withBlock:^id(id jsonValue) {
54+
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
55+
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
56+
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
57+
58+
NSDate *date = nil;
59+
NSError *error = nil;
60+
if (![dateFormatter getObjectValue:&date forString:jsonValue range:nil error:&error]) {
61+
NSLog(@"Date '%@' could not be parsed: %@", jsonValue, error);
62+
}
63+
64+
return date;
65+
}];
66+
67+
````
5068
5169
##### Usage
5270
@@ -79,27 +97,6 @@ it simply add the following line to your Podfile:
7997
8098
````
8199

82-
##### Using `JSONAPIResourceFormatter` to format values
83-
Below is an example to register a "Date" function to format a date in a NSString object to an NSDate object before its mapped to the JSONAPIResource instance.
84-
85-
````objc
86-
87-
[JSONAPIResourceFormatter registerFormat:@"Date" withBlock:^id(id jsonValue) {
88-
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
89-
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
90-
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
91-
92-
NSDate *date = nil;
93-
NSError *error = nil;
94-
if (![dateFormatter getObjectValue:&date forString:jsonValue range:nil error:&error]) {
95-
NSLog(@"Date '%@' could not be parsed: %@", jsonValue, error);
96-
}
97-
98-
return date;
99-
}];
100-
101-
````
102-
103100
### JSONAPIResourceLinker
104101
`JSONAPIResourceLinker` is used for configuring the type of 'links' resources to 'linked' resources.
105102

0 commit comments

Comments
 (0)