Created
April 7, 2016 09:37
-
-
Save rawrjustin/79f5186717fbc38c0b617a390ab9c0f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Swift Extension on MTLModel | |
extension MTLModel: Parseable { | |
public static func parseResponseObject(responseObject: AnyObject) throws -> AnyObject { | |
if let JSON = responseObject as? Array<AnyObject> { | |
do { | |
return try MTLJSONAdapter.modelsOfClass(self, fromJSONArray: JSON) as! [MTLModel] | |
} catch { | |
throw BridgeErrorType.Parsing | |
} | |
} else if let JSON = responseObject as? Dictionary<NSObject, AnyObject> { | |
do { | |
return try MTLJSONAdapter.modelOfClass(self, fromJSONDictionary: JSON) as! MTLModel | |
} catch { | |
throw BridgeErrorType.Parsing | |
} | |
} | |
throw BridgeErrorType.Parsing | |
} | |
} | |
// Objective C Model | |
#import <Mantle/Mantle.h> | |
@interface GithubUser : MTLModel<MTLJSONSerializing> | |
@property (nonatomic, copy) NSString *login; | |
@property (nonatomic, copy) NSNumber *id; | |
@property (nonatomic, copy) NSURL *avatarURL; | |
@property (nonatomic, copy) NSString *name; | |
@end | |
#import "GithubUser.h" | |
@implementation GithubUser | |
+ (NSDictionary *)JSONKeyPathsByPropertyKey { | |
return @{ | |
@"id": @"id", | |
@"login": @"login", | |
@"avatarURL": @"avatar_url", | |
@"name": @"name" | |
}; | |
} | |
+ (NSValueTransformer *)avatarURLJSONTransformer { | |
return [NSValueTransformer valueTransformerForName:MTLURLValueTransformerName]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment