Created
May 27, 2009 20:15
-
-
Save afarnham/118880 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
#import <Foundation/Foundation.h> | |
@interface Square : NSObject { | |
NSNumber *width; | |
NSNumber *height; | |
CGPoint center; | |
} | |
- (id)initWithJSON:(NSDictionary *)jsonData; | |
@end | |
@implementation Square | |
- (id)initWithJSON:(NSDictionary *)jsonData { | |
self = [super init]; | |
if (self != nil) { | |
width = [jsonData objectForKey:@"width"]; | |
height = [jsonData objectForKey:@"height"]; | |
center.x = [[[jsonData objectForKey:@"center"] objectAtIndex:0] floatValue]; | |
center.y = [[[jsonData objectForKey:@"center"] objectAtIndex:1] floatValue]; | |
} | |
return self; | |
} | |
- (NSString *)description { | |
return [NSString stringWithFormat: | |
@"I am square with width %d, height %d, and center %f,%f", | |
[width intValue], [height intValue], center.x, center.y]; | |
} | |
@end | |
@interface Circle : NSObject { | |
NSNumber *radius; | |
CGPoint center; | |
} | |
- (id)initWithJSON:(NSDictionary *)jsonData; | |
@end | |
@implementation Circle | |
- (id)initWithJSON:(NSDictionary *)jsonData { | |
self = [super init]; | |
if (self != nil) { | |
radius = [jsonData objectForKey:@"radius"]; | |
center.x = [[[jsonData objectForKey:@"center"] objectAtIndex:0] floatValue]; | |
center.y = [[[jsonData objectForKey:@"center"] objectAtIndex:1] floatValue]; | |
} | |
return self; | |
} | |
- (NSString *)description { | |
return [NSString stringWithFormat: | |
@"I am circle with radius %d, and center %f,%f", | |
[radius intValue], center.x, center.y]; | |
} | |
@end | |
@interface ResponseHandler : NSObject { | |
NSArray *dataObjs; | |
} | |
@property (nonatomic, retain)NSArray *dataObjs; | |
- (id)initWithShapesData:(NSDictionary *)jsonData; | |
@end | |
@implementation ResponseHandler | |
@synthesize dataObjs; | |
- (id)initWithShapesData:(NSDictionary *)jsonData { | |
self = [super init]; | |
if (self != nil) { | |
NSDictionary *classMap = [NSDictionary dictionaryWithObjectsAndKeys: | |
[Square class], @"SquareObject", [Circle class], @"CircleObject", nil]; | |
NSMutableArray *tmpObjs = [[NSMutableArray alloc] init]; | |
NSObject *anObj; | |
for (NSString *aKey in [classMap allKeys]) { | |
anObj = [[[classMap objectForKey:aKey] alloc] initWithJSON:[jsonData objectForKey:aKey]]; | |
[tmpObjs addObject:anObj]; | |
[anObj release]; | |
} | |
self.dataObjs = [NSArray arrayWithArray:tmpObjs]; | |
[tmpObjs release]; | |
} | |
return self; | |
} | |
@end | |
NSDictionary *mockJSONData(){ | |
//Pretend the original JSON dictionary was previously converted to an NSDictionary for us with TouchJSON | |
//Original JSON was: "{'SquareObject': {'width': 10, 'height':10, 'center':[5,5]}, 'CircleObject': {'radius':10, 'center':[2,2]}}" | |
NSArray *squareCenter = [NSArray arrayWithObjects:[NSNumber numberWithInt:5],[NSNumber numberWithInt:5], nil]; | |
NSArray *circleCenter = [NSArray arrayWithObjects:[NSNumber numberWithInt:2],[NSNumber numberWithInt:2], nil]; | |
NSDictionary *sqr = [NSDictionary dictionaryWithObjectsAndKeys: | |
[NSNumber numberWithInt:10], @"width", [NSNumber numberWithInt:10], @"height", squareCenter, @"center", nil]; | |
NSDictionary *cir = [NSDictionary dictionaryWithObjectsAndKeys: | |
[NSNumber numberWithInt:10], @"radius", circleCenter, @"center", nil]; | |
//jsonData is the object we would have received from TouchJSON | |
NSDictionary *jsonData = [NSDictionary dictionaryWithObjectsAndKeys:sqr, @"SquareObject", cir, @"CircleObject", nil]; | |
return jsonData; | |
} | |
int main (int argc, const char * argv[]) { | |
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; | |
NSDictionary *shapesData = mockJSONData(); | |
ResponseHandler *response = [[ResponseHandler alloc] initWithShapesData:shapesData]; | |
for (NSObject *anObj in response.dataObjs) { | |
NSLog(@"%@", anObj); | |
} | |
[pool drain]; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment