Skip to content

Instantly share code, notes, and snippets.

@mungler
Created March 19, 2019 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mungler/cf986dbb2cbbad3e044ccc180048008c to your computer and use it in GitHub Desktop.
Save mungler/cf986dbb2cbbad3e044ccc180048008c to your computer and use it in GitHub Desktop.
A custom parser class for KeyValueObjectMapping to support j2objc'd IOSObjectArray type
//
// DCIOSObjectArrayConverter.m
//
// Created by Rory Sinclair on 13/03/2019.
// Copyright © 2019 ASMALLWORLD. All rights reserved.
//
#import "DCIOSObjectArrayConverter.h"
#import "IOSObjectArray.h"
#import "DCDynamicAttribute.h"
#import "DCNSArrayConverter.h"
@interface DCIOSObjectArrayConverter()
@property(nonatomic, strong) DCParserConfiguration *configuration;
@property(nonatomic, strong) DCNSArrayConverter *arrayConverter;
@end
@implementation DCIOSObjectArrayConverter
@synthesize configuration = _configuration;
@synthesize arrayConverter = _arrayConverter;
+ (DCIOSObjectArrayConverter *) iOSObjectArrayConverterForConfiguration: (DCParserConfiguration *)configuration {
return [[self alloc] initWithConfiguration: configuration];
}
- (id)initWithConfiguration:(DCParserConfiguration *)configuration{
self = [super init];
if (self) {
self.configuration = configuration;
self.arrayConverter = [DCNSArrayConverter arrayConverterForConfiguration:self.configuration];
}
return self;
}
- (id)transformValue:(id)value forDynamicAttribute:(DCDynamicAttribute *)attribute dictionary:(NSDictionary *)dictionary parentObject:(id)parentObject {
NSMutableArray *result = [NSMutableArray array];
for (id object in (IOSObjectArray *)value) {
if (object == nil || object == (id)[NSNull null]) {
// NSLog(@"Skipping null object in array");
} else {
[result addObject:object];
}
}
return [self.arrayConverter transformValue:[NSArray arrayWithArray:result] forDynamicAttribute:attribute dictionary:dictionary parentObject:parentObject];
}
- (id)serializeValue:(id)value forDynamicAttribute:(DCDynamicAttribute *)attribute {
NSException* myException = [NSException
exceptionWithName:@"NotSupportedException"
reason:@"serializeValue:forDynamicAttribute without dictionary: and parentObject: is not supported on iOS."
userInfo:nil];
@throw myException;
}
- (BOOL)canTransformValueForClass:(Class)cls {
return [cls isSubclassOfClass:[IOSObjectArray class]];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment