mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-01 22:42:51 +08:00
* Removed RestKit from inheritance hierarchy * Mappings are implemented as concrete classes * Mapper is much more flexible & powerful * Much more robust error handling * Serialization is reimplemented as an object mapping operation * Added ability to serialize to JSON natively * Reworked Core Data integration * Simplified the codebase substantially
71 lines
2.2 KiB
Objective-C
71 lines
2.2 KiB
Objective-C
//
|
|
// RKObjectMappingResult.m
|
|
// RestKit
|
|
//
|
|
// Created by Blake Watters on 5/7/11.
|
|
// Copyright 2011 Two Toasters. All rights reserved.
|
|
//
|
|
|
|
#import "RKObjectMappingResult.h"
|
|
#import "RKObjectMapperError.h"
|
|
|
|
@implementation RKObjectMappingResult
|
|
|
|
- (id)initWithDictionary:(id)dictionary {
|
|
self = [self init];
|
|
if (self) {
|
|
_keyPathToMappedObjects = [dictionary retain];
|
|
}
|
|
|
|
return self;
|
|
}
|
|
|
|
- (void)dealloc {
|
|
[_keyPathToMappedObjects release];
|
|
[super dealloc];
|
|
}
|
|
|
|
+ (RKObjectMappingResult*)mappingResultWithDictionary:(NSDictionary*)keyPathToMappedObjects {
|
|
return [[[self alloc] initWithDictionary:keyPathToMappedObjects] autorelease];
|
|
}
|
|
|
|
- (NSDictionary*)asDictionary {
|
|
return _keyPathToMappedObjects;
|
|
}
|
|
|
|
- (NSArray*)asCollection {
|
|
// Flatten results down into a single array
|
|
NSMutableArray* collection = [NSMutableArray array];
|
|
for (id object in [_keyPathToMappedObjects allValues]) {
|
|
// We don't want to strip the keys off of a mapped dictionary result
|
|
|
|
if (NO == [object isKindOfClass:[NSDictionary class]] && [object respondsToSelector:@selector(allObjects)]) {
|
|
[collection addObjectsFromArray:[object allObjects]];
|
|
} else {
|
|
[collection addObject:object];
|
|
}
|
|
}
|
|
|
|
return collection;
|
|
}
|
|
|
|
- (id)asObject {
|
|
// TODO: Warn that only last object was returned...
|
|
return [[self asCollection] objectAtIndex:0];
|
|
}
|
|
|
|
- (NSError*)asError {
|
|
NSArray* collection = [self asCollection];
|
|
// TODO: What is the correct behavior when there is an empty collection we expect to contain an error???
|
|
NSAssert([collection count] > 0, @"Expected mapping result to contain at least one object to construct an error");
|
|
NSString* description = [[collection valueForKeyPath:@"description"] componentsJoinedByString:@", "];
|
|
|
|
NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys:description, NSLocalizedDescriptionKey,
|
|
collection, RKObjectMapperErrorObjectsKey, nil];
|
|
|
|
NSError* error = [NSError errorWithDomain:RKRestKitErrorDomain code:RKObjectMapperErrorFromMappingResult userInfo:userInfo];
|
|
return error;
|
|
}
|
|
|
|
@end
|