Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Working with Dictionaries. This example shows how to pass a dictionary between two objects (in this case 2 workspaces). In the main workspace we build the dictionary. In the second workspace we read from the dictionary and build a shape with its variables.
//
// C4WorkSpace.m
// C4Code
//
// Created by Travis Kirton on 11/4/2013.
//
#import "MyWorkSpace.h"
@interface C4WorkSpace ()
@property (nonatomic) MyWorkSpace *myWorkSpace;
@end
@implementation C4WorkSpace
-(void)setup {
self.myWorkSpace = [[MyWorkSpace alloc] init];
self.myWorkSpace.canvas.frame = CGRectMake(0, 0, 384, 256);
self.myWorkSpace.canvas.center = self.canvas.center;
[self.myWorkSpace setup];
[self.canvas addSubview:self.myWorkSpace.canvas];
C4Shape *circle = [C4Shape ellipse:CGRectMake(0, 0, 100, 100)];
circle.center = CGPointMake(self.canvas.center.x, 75);
circle.fillColor = C4RED;
circle.strokeColor = C4BLUE;
circle.lineWidth = 20.0f;
[self.canvas addShape:circle];
NSDictionary *shapeDictionary = @{
@"size":[NSValue valueWithCGSize:circle.frame.size],
@"fillColor":circle.fillColor,
@"strokeColor":circle.strokeColor,
@"lineWidth":@(circle.lineWidth)
};
self.myWorkSpace.shapeDictionary = shapeDictionary;
}
-(BOOL)prefersStatusBarHidden {
return YES;
}
@end
//
// MyWorkSpace.h
// C4Code
//
// Created by moi on 11/4/2013.
// Copyright (c) 2013 moi. All rights reserved.
//
#import "C4WorkSpace.h"
@interface MyWorkSpace : C4WorkSpace
@property (nonatomic) NSDictionary *shapeDictionary;
@end
//
// MyWorkSpace.m
// C4Code
//
// Created by moi on 11/4/2013.
// Copyright (c) 2013 moi. All rights reserved.
//
#import "MyWorkSpace.h"
@interface MyWorkSpace ()
@end
@implementation MyWorkSpace
-(void)setup {
self.canvas.backgroundColor = C4GREY;
}
-(void)setShapeDictionary:(NSDictionary *)shapeDictionary {
_shapeDictionary = shapeDictionary;
CGSize size = [shapeDictionary[@"size"] CGSizeValue];
//I change it to a rect here...
C4Shape *newShape = [C4Shape rect:CGRectMake(0, 0, size.width, size.height)];
newShape.fillColor = shapeDictionary[@"fillColor"];
newShape.strokeColor = shapeDictionary[@"strokeColor"];
newShape.lineWidth = [shapeDictionary[@"lineWidth"] floatValue];
newShape.center = CGPointMake(self.canvas.width / 2.0f, self.canvas.height / 2.0f);
[self.canvas addShape:newShape];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment