Skip to content

Commit 30a8a45

Browse files
committed
Added comments around the case where we get historicalPrices as an object and not an array of objects.
1 parent 003fb2a commit 30a8a45

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

KVC Validation Pattern/KVC Validation Pattern/CTCViewController.m

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ - (NSString*)jsonStringForResponse:(NSUInteger)responseIndex {
7676

7777
#pragma mark - UI response
7878

79-
- (IBAction)viewJSONTapped:(id)sender {
80-
NSLog(@"View JSON tapped");
81-
79+
- (IBAction)viewJSONTapped:(id)sender {
8280
// toggle source view
8381
if (self.sourceTextView.hidden == YES) {
8482
self.sourceTextView.hidden = NO;

KVC Validation Pattern/KVC Validation Pattern/Model/CTCStation.m

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,23 @@ - (NSDictionary *)undefinedKeys {
3333

3434
- (BOOL)validateHistoricalPrices:(id *)ioValue error:(NSError *__autoreleasing *)outError{
3535
dispatch_once(&_historyToken, ^{
36-
_historyValidator = [[CTCArrayTypeValidator alloc] initWithPostValidation:^NSArray *(id value){
36+
// if this happens to be a single object (i.e. a dictionary) then assume it was supposed to be an array of one element
37+
_historyValidator = [[CTCArrayTypeValidator alloc] initWithDefaultValidation:^NSArray *(id value, BOOL *isValid, NSError **error){
38+
if ([value isKindOfClass:[NSDictionary class]]){
39+
*isValid = YES;
40+
return @[value];
41+
}
42+
return nil;
43+
}];
44+
45+
// take the array we are given and process its contents as CTCHistoricalPrice objects
46+
_historyValidator.postValidation = ^NSArray *(id value){
3747
NSMutableArray *histories = [NSMutableArray array];
3848
for (NSDictionary *dict in value){
3949
CTCHistoricalPrice *price = [[CTCHistoricalPrice alloc] initWithDictionary:dict];
4050
[histories addObject:price];
4151
}
4252
return histories;
43-
}];
44-
_historyValidator.defaultValidation = ^NSArray *(id value, BOOL *isValid, NSError **error){
45-
if ([value isKindOfClass:[NSDictionary class]]){
46-
*isValid = YES;
47-
return @[value];
48-
}
49-
return nil;
5053
};
5154
});
5255

0 commit comments

Comments
 (0)