Posted By

phifty on 08/11/09


Tagged

objc KVC


Versions (?)

Lite KVC Wrapper


 / Published in: Objective C
 

This is a wrapper for objects that use Key-Value Coding (KVC) in Objective-C. An appropriate use would be for business objects whose state changes throughout the life of the application session.

  1. //
  2. // KVCEnabledObject.h
  3. //
  4.  
  5. #import <Foundation/Foundation.h>
  6.  
  7.  
  8. @interface KVCEnabledObject : NSObject {
  9. NSMutableDictionary *_metadata;
  10. }
  11.  
  12. @end
  13.  
  14. @implementation KVCEnabledObject
  15.  
  16. - (id)init
  17. {
  18. if (self = [super init]) {
  19. _metadata = [[NSMutableDictionary alloc] initWithCapacity:0];
  20. }
  21. }
  22.  
  23. - (void)setValue:(id)value forUndefinedKey:(NSString *)key
  24. {
  25. if (nil != value) {
  26. [_metadata setValue:value forKey:key];
  27. }
  28. }
  29.  
  30. - (void)setNilValueForKey:(NSString *)theKey
  31. {
  32. [_metadata setValue:[NSNull null] forKey:key];
  33. }
  34.  
  35. - (id)valueForUndefinedKey:(NSString *)key
  36. {
  37. id ret = [_metadata valueForKey:key];
  38. return ([ret isKindOfClass:[NSNull class]]) ? nil : ret;
  39. }
  40.  
  41. - (void)dealloc
  42. {
  43. [_metadata removeAllObjects];
  44. [_metadata release];
  45.  
  46. [super dealloc];
  47. }
  48.  
  49. @end

Report this snippet  

You need to login to post a comment.