/
TCExtendableObject.m
58 lines (46 loc) · 1.45 KB
/
TCExtendableObject.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//
// TCExtendableObject.m
// Technicolor
//
// Created by Steve Streza on 2/14/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "TCExtendableObject.h"
@implementation TCExtendableObject
- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context{
if(self = [super initWithEntity:entity insertIntoManagedObjectContext:context]){
[self _setupPropertyDictionary];
}
return self;
}
-(void)_setupPropertyDictionary{
if(props) return;
NSData *data = [self valueForKey:@"extraProperties"];
if(data){
NSDictionary *dict = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(@"Whee! %@",dict);
props = [dict mutableCopy];
}else{
props = [[NSMutableDictionary alloc] init];
}
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
NSLog(@"-[TCExtendableObject setValue: %@ forUndefinedKey: %@]",value,key);
[self _setupPropertyDictionary];
[props setObject:value forKey:key];
}
- (id)valueForUndefinedKey:(NSString *)key{
NSLog(@"-[TCExtendableObject valueForUndefinedKey: %@]",key);
return [props valueForKey:key];
}
- (BOOL)validateValue:(id *)value forKey:(NSString *)key error:(NSError **)error{
*error = nil;
// NSLog(@"Validating %@ with %@",key,*value);
if([key isEqualToString:@"extraProperties"] && props){
*value = [NSKeyedArchiver archivedDataWithRootObject:props];
// NSLog(@"Archived - %@",*value);
return YES;
}
return YES;
}
@end