home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / Database / OTC_EOFBetaExamples_V1.0 / EOFramework / KeyValueCoding / Catalog.m < prev    next >
Encoding:
Text File  |  1994-07-31  |  5.8 KB  |  201 lines

  1. /*--------------------------------------------------------------------------
  2.  *
  3.  *     You may freely copy, distribute, and reuse the code in this example.
  4.  *     SHL Systemhouse disclaims any warranty of any kind, expressed or  
  5.  *    implied, as to its fitness for any particular use.
  6.  *
  7.  *    Catalog
  8.  *
  9.  *    Inherits From:        NSObject
  10.  *
  11.  *    Conforms To:        None
  12.  *
  13.  *    Declared In:        Catalog.h
  14.  *
  15.  *------------------------------------------------------------------------*/
  16. #import "Catalog.h"
  17. #import "AppController.h"
  18.  
  19.  
  20. #define print_trace(arg,isSet) [[NXApp delegate] perform: \
  21.         @selector(console:) withObject: \
  22.         [NSArray arrayWithObjects:arg, \
  23.             [NSValue value:&isSet withObjCType:"char"], nil]]
  24. #define display(arg, isSet) [[NXApp delegate] perform: \
  25.         @selector(display:) withObject: \
  26.         [NSArray arrayWithObjects:arg, \
  27.         [NSValue value:&isSet withObjCType:"char"], nil]]
  28.  
  29.  
  30. #define ACCESSOR_SET_TEXT "\n\n\n\n\(1) takeValuesFromDictionary:\n\
  31. (2) %s\n––––––––––––––––––––––––>"
  32. #define ACCESSOR_GET_TEXT "\n\n\n\n(1) valuesForKeys:\n\
  33. (2) %s\n<––––––––––––––––––––––––"
  34. #define IVAR_SET_TEXT "(1) takeValuesFromDictionary:\n\
  35. (2) matched instance variable\n––––––––––––––––––––––––>"
  36. #define IVAR_GET_TEXT "(1) valuesForKeys:\n\
  37. (2) matched instance variable\n<––––––––––––––––––––––––"
  38. #define KVC_SET_TEXT "\n\n\n(1) takeValuesFromDictionary:\n\
  39. ––––––––––––––––––––––––>"
  40. #define KVC_GET_TEXT "\n\n\n(1) valuesForKeys:\n\
  41. <––––––––––––––––––––––––"
  42.  
  43.  
  44.  
  45.  
  46. @implementation Catalog
  47.  
  48. /*--------------------------------------------------------------------------
  49.  *    Initializing
  50.  *------------------------------------------------------------------------*/
  51. - (void) dealloc
  52. {
  53.     if (fullname) [fullname release];
  54.     if (price) [price release];
  55.     if (volumeTitle) [volumeTitle release];
  56.     return [super dealloc];
  57. }
  58.  
  59.  
  60. /*--------------------------------------------------------------------------
  61.  *    Accessor methods
  62.  *------------------------------------------------------------------------*/
  63. - authorName
  64. {
  65.     return fullname;
  66. }
  67.  
  68.  
  69. - volumeTitle
  70. {
  71.     return volumeTitle;
  72. }
  73.  
  74.  
  75. - price 
  76. {
  77.     id        newString, formatString;
  78.     BOOL    isSet = NO;
  79.  
  80.     formatString = [NSString stringWithCString: ACCESSOR_GET_TEXT];    
  81.     newString = [NSString stringWithFormat: formatString, sel_getName(_cmd)];
  82.  
  83.     print_trace(@"(1) valuesForKeys:\n(2) price\n", isSet);
  84.     display(newString, isSet);
  85.  
  86.     return price; 
  87. }
  88.  
  89.  
  90. - setPrice: aPrice
  91. {
  92.     id        newString, formatString;
  93.     BOOL    isSet = YES;
  94.  
  95. /*--------------------------------------------------------------------------
  96.  *    the class check is to work around an association bug where different
  97.  *     objects are passed in when value is read from the dataStore vs. when it 
  98.  *     is read from the user interface.
  99.  *------------------------------------------------------------------------*/     
  100.     if ([aPrice isKindOfClass: [NSNumber class]])
  101.         price = [[NSNumber numberWithDouble:
  102.                     ([aPrice doubleValue] + 10.0)] retain];
  103.     else price = [[NSNumber numberWithFloat: [aPrice floatValue]] retain];
  104.  
  105.  
  106.     formatString = [NSString stringWithCString: ACCESSOR_SET_TEXT];    
  107.     newString = [NSString stringWithFormat: formatString, sel_getName(_cmd)];
  108.  
  109.     [[NXApp delegate] updateObjectView:self];
  110.     print_trace(@"(1) takeValuesFromDictionary:\n(2) setPrice:\n", isSet);
  111.     display(newString, isSet);
  112.  
  113.     return self;
  114. }
  115.  
  116.  
  117. /*--------------------------------------------------------------------------
  118.  *    Key Value Coding protocol
  119.  *------------------------------------------------------------------------*/
  120. - (BOOL)takeValuesFromDictionary:(NSDictionary *)dictionary
  121. {
  122.     id         object, newString;
  123.     BOOL    isSet = YES;
  124.  
  125.      [super takeValuesFromDictionary: dictionary];
  126.  
  127. /*--------------------------------------------------------------------------
  128.  *    instance variables were set in the call to super, but could not be 
  129.  *    displayed visually.  we'll do it now.
  130.  *------------------------------------------------------------------------*/
  131.     if (object = [dictionary objectForKey: @"fullname"]) {
  132.         newString = [NSString stringWithCString: IVAR_SET_TEXT];    
  133.  
  134.         [[NXApp delegate] updateObjectView:self];
  135.         print_trace(
  136.             @"(1) takeValuesFromDictionary:\n(2) matched instance variable\n",
  137.             isSet);
  138.         display(newString, isSet);
  139.     }
  140.  
  141. /*--------------------------------------------------------------------------
  142.  *    any key/value pairs that are handled outside of the accessor / ivar
  143.  *     paradigm must be handled here.
  144.  *------------------------------------------------------------------------*/
  145.     if (object = [dictionary objectForKey: @"title"]) {
  146.         volumeTitle = [object retain];
  147.  
  148.         newString = [NSString stringWithCString: KVC_SET_TEXT];    
  149.  
  150.         [[NXApp delegate] updateObjectView:self];
  151.         print_trace(@"(1) takeValuesFromDictionary:\n",
  152.                         isSet);
  153.         display(newString, isSet);
  154.     }
  155.  
  156.     print_trace(@"–––––––––\n", isSet);
  157.     return YES;
  158. }
  159.  
  160.  
  161. - (NSDictionary *)valuesForKeys:(NSArray *)keys
  162. {
  163.     id     dictionary,    newString;
  164.     BOOL    isSet = NO;
  165.  
  166.  
  167.     dictionary = [super valuesForKeys:keys];    
  168.  
  169. /*--------------------------------------------------------------------------
  170.  *    all instance variables were set in a call to super, but could not
  171.  *    be displayed.  we'll do it now.
  172.  *------------------------------------------------------------------------*/
  173.     if ([keys containsObject: @"fullname"]) {
  174.         newString = [NSString stringWithCString: IVAR_GET_TEXT];    
  175.         print_trace(@"(1) valuesForKeys:\n(2) matched instance variable\n",
  176.             isSet);
  177.         display(newString, isSet);
  178.     }
  179.     
  180.  
  181. /*--------------------------------------------------------------------------
  182.  *    any key / value pairs that are not handled via accessors or ivars
  183.  *    can be specially handled here.  we'll display that now.
  184.  *------------------------------------------------------------------------*/
  185.     if ([keys containsObject: @"title"]) {
  186.         [dictionary setObject: volumeTitle forKey: @"title"];
  187.  
  188.         newString = [NSString stringWithCString: KVC_GET_TEXT];    
  189.  
  190.         print_trace(@"(1) valuesForKeys:\n", isSet);
  191.         display(newString, isSet);
  192.     }
  193.  
  194.     print_trace(@"–––––––––\n", isSet);
  195.  
  196.     return dictionary;
  197. }
  198.  
  199.  
  200. @end
  201.