Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fb93272

Browse files
author
Blake Watters
committed
Migrate RKValueTransformers and ISO8601DateFormatter into standalone libraries
1 parent 10ac704 commit fb93272

17 files changed

Lines changed: 131 additions & 3580 deletions

.gitmodules

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,9 @@
44
[submodule "Vendor/TransitionKit"]
55
path = Vendor/TransitionKit
66
url = https://github.com/blakewatters/TransitionKit.git
7+
[submodule "Vendor/RKValueTransformers"]
8+
path = Vendor/RKValueTransformers
9+
url = [email protected]:RestKit/RKValueTransformers.git
10+
[submodule "Vendor/ISO8601DateFormatterValueTransformer"]
11+
path = Vendor/ISO8601DateFormatterValueTransformer
12+
url = [email protected]:blakewatters/ISO8601DateFormatterValueTransformer.git

Code/ObjectMapping/RKMappingOperation.m

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,7 @@ - (BOOL)shouldSetValue:(id *)value forKeyPath:(NSString *)keyPath usingMapping:(
360360
361361
See issue & pull request: https://github.com/RestKit/RestKit/pull/436
362362
*/
363-
if (*value == [NSNull null]) {
364-
RKLogWarning(@"Coercing NSNull value to nil in shouldSetValue:atKeyPath: -- should be fixed.");
365-
*value = nil;
366-
}
363+
if (*value == [NSNull null]) *value = nil;
367364

368365
if (nil == currentValue && nil == *value) {
369366
// Both are nil

Code/ObjectMapping/RKObjectMapping.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#import "RKMacros.h"
2222
#import "RKMapping.h"
23+
#import "RKValueTransformers.h"
2324

2425
@class RKPropertyMapping, RKAttributeMapping, RKRelationshipMapping;
2526
@protocol RKValueTransforming;
@@ -474,3 +475,29 @@
474475
@property (nonatomic, strong) NSFormatter *preferredDateFormatter DEPRECATED_ATTRIBUTE_MESSAGE("Use `valueTransformer` instead");
475476

476477
@end
478+
479+
///----------------
480+
/// @name Functions
481+
///----------------
482+
483+
/**
484+
Returns an date representation of a given string value by attempting to parse the string with all default date formatters in turn.
485+
486+
@param dateString A string object encoding a date value.
487+
@return An `NSDate` object parsed from the given string, or `nil` if the string was found to be unparsable by all default date formatters.
488+
@see [RKObjectMapping defaultDateFormatters]
489+
*/
490+
NSDate *RKDateFromString(NSString *dateString);
491+
492+
/**
493+
Returns a string representation of a given date formatted with the preferred date formatter.
494+
495+
This is a convenience function that is equivalent to the following example code:
496+
497+
NSString *string = [[RKObjectMapping preferredDateFormatter] stringForObjectValue:date]
498+
499+
@param date The date object to be formatted.
500+
@return An `NSString` object representation of the given date formatted by the preferred date formatter.
501+
@see [RKObjectMapping preferredDateFormatter]
502+
*/
503+
NSString *RKStringFromDate(NSDate *date);

Code/ObjectMapping/RKObjectMapping.m

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
#import "RKRelationshipMapping.h"
2424
#import "RKPropertyInspector.h"
2525
#import "RKLog.h"
26-
#import "RKISO8601DateFormatter.h"
2726
#import "RKAttributeMapping.h"
2827
#import "RKRelationshipMapping.h"
2928
#import "RKValueTransformers.h"
29+
#import "ISO8601DateFormatterValueTransformer.h"
3030

3131
typedef NSString * (^RKSourceToDesinationKeyTransformationBlock)(RKObjectMapping *, NSString *);
3232

@@ -130,6 +130,13 @@ + (RKObjectMapping *)requestMapping
130130
return [self mappingForClass:[NSMutableDictionary class]];
131131
}
132132

133+
+ (void)initialize
134+
{
135+
// Add an ISO8601DateFormatter to the transformation stack for backwards compatibility
136+
RKISO8601DateFormatter *dateFormatter = [RKISO8601DateFormatter defaultISO8601DateFormatter];
137+
[[RKValueTransformer defaultValueTransformer] insertValueTransformer:dateFormatter atIndex:0];
138+
}
139+
133140
- (id)initWithClass:(Class)objectClass
134141
{
135142
self = [super init];
@@ -519,3 +526,21 @@ - (void)setDateFormatters:(NSArray *)dateFormatters
519526
@end
520527

521528
#pragma clang diagnostic pop
529+
530+
#pragma mark - Functions
531+
532+
NSDate *RKDateFromString(NSString *dateString)
533+
{
534+
NSDate *outputDate = nil;
535+
NSError *error = nil;
536+
BOOL success = [[RKValueTransformer defaultValueTransformer] transformValue:dateString toValue:&outputDate ofClass:[NSDate class] error:&error];
537+
return success ? outputDate : nil;
538+
}
539+
540+
NSString *RKStringFromDate(NSDate *date)
541+
{
542+
NSString *outputString = nil;
543+
NSError *error = nil;
544+
BOOL success = [[RKValueTransformer defaultValueTransformer] transformValue:date toValue:&outputString ofClass:[NSString class] error:&error];
545+
return success ? outputString : nil;
546+
}

0 commit comments

Comments
 (0)