Skip to content

Instantly share code, notes, and snippets.

@johnclayton
Created February 22, 2012 16:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnclayton/1885738 to your computer and use it in GitHub Desktop.
Save johnclayton/1885738 to your computer and use it in GitHub Desktop.
An implementation of Ruby's OStruct in Objective C
//
// FSQOpenStruct.h
// FivesquareKit
//
// Created by John Clayton on 2/5/12.
// Copyright (c) 2012 Fivesquare Software, LLC. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface FSQOpenStruct : NSObject
@property (strong, readonly) NSMutableDictionary *attributes;
@end
//
// FSQOpenStruct.m
// FivesquareKit
//
// Created by John Clayton on 2/5/12.
// Copyright (c) 2012 Fivesquare Software, LLC. All rights reserved.
//
#import "FSQOpenStruct.h"
#import "NSDictionary+FSQFoundation.h"
@implementation FSQOpenStruct
@synthesize attributes=attributes_;
- (NSMutableDictionary *) attributes {
@synchronized(self) {
if (nil == attributes_) {
attributes_ = [NSMutableDictionary new];
}
}
return attributes_;
}
- (id)initWithAttributes:(NSDictionary *)attributes {
self = [super init];
if (self) {
attributes_ = [attributes mutableDeepCopy];
}
return self;
}
- (NSString *) description {
return [NSString stringWithFormat:@"%@ : %@", [super description], [self.attributes description]];
}
// ========================================================================== //
#pragma mark - KVC
- (id)valueForKey:(NSString *)key {
id value = [self.attributes valueForKey:key];
if (nil == value) {
value = [NSMutableDictionary new];
[self.attributes setObject:value forKey:key];
}
return value;
}
- (void)setValue:(id)value forKey:(NSString *)key {
[self willChangeValueForKey:key];
[self.attributes setValue:value forKey:key];
[self didChangeValueForKey:key];
}
- (NSDictionary *)dictionaryWithValuesForKeys:(NSArray *)keys {
return [self.attributes dictionaryWithValuesForKeys:keys];
}
- (void)setValuesForKeysWithDictionary:(NSDictionary *)keyedValues {
for (NSString *key in keyedValues) {
[self willChangeValueForKey:key];
}
[self.attributes setValuesForKeysWithDictionary:keyedValues];
for (NSString *key in keyedValues) {
[self didChangeValueForKey:key];
}
}
//
// NSDictionary+FSQFoundation.h
// FivesquareKit
//
// Created by John Clayton on 2/5/12.
// Copyright (c) 2012 Fivesquare Software, LLC. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSDictionary (FSQFoundation)
- (NSDictionary *) deepCopy;
- (NSMutableDictionary *) mutableDeepCopy;
@end
//
// NSDictionary+FSQFoundation.m
// FivesquareKit
//
// Created by John Clayton on 2/5/12.
// Copyright (c) 2012 Fivesquare Software, LLC. All rights reserved.
//
#import "NSDictionary+FSQFoundation.h"
@implementation NSDictionary (FSQFoundation)
- (NSDictionary *) deepCopy {
NSMutableDictionary *deepCopy = [[NSMutableDictionary alloc] init];
for (NSString *key in self) {
id value = [self valueForKey:key];
[deepCopy setValue:[value copy] forKey:key];
}
return deepCopy;
}
- (NSMutableDictionary *) mutableDeepCopy {
return (NSMutableDictionary *)[self deepCopy];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment