Advertisement
Leonne

An Example of KVC and KVO.

Sep 13th, 2012
1,408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import <Foundation/Foundation.h>
  2.  
  3. @interface Character : NSObject
  4. {
  5.     NSString *characterName;
  6.     NSInteger ownedClowCards;
  7. }
  8. @property (nonatomic, copy) NSString *characterName;
  9. @property NSInteger ownedClowCards;
  10. -(void)hasLostClowCard;
  11. -(void)hasGainedClowCard;
  12. @end
  13.  
  14. @implementation Character
  15. @synthesize characterName;
  16. @synthesize ownedClowCards;
  17.  
  18. -(void)hasLostClowCard
  19. {
  20.     NSLog(@"%@ has lost a card! Cards now: %ld", characterName, ownedClowCards);
  21. }
  22.  
  23. -(void)hasGainedClowCard
  24. {
  25.     NSLog(@"%@ has earned a card! Cards now: %ld", characterName, ownedClowCards);
  26. }
  27.  
  28. -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  29. {
  30.     if([keyPath isEqualToString:@"ownedClowCards"])
  31.     {
  32.         NSInteger oldC = [change objectForKey:NSKeyValueChangeNewKey];
  33.         NSInteger newC = [change objectForKey:NSKeyValueChangeOldKey];
  34.         if(oldC < newC)
  35.         {
  36.             [self hasLostClowCard];
  37.         }else
  38.         {
  39.             [self hasGainedClowCard];
  40.         }
  41.     }
  42. }
  43.  
  44. @end
  45.  
  46. int main()
  47. {
  48.     Character *sakura;
  49.     Character *shaoran;
  50.    
  51.     //Create and give the properties some values with KVC...
  52.    
  53.     sakura = [[Character alloc] init];
  54.     [sakura setValue:@"Sakura Kinomoto" forKey:@"characterName"];
  55.     [sakura setValue:[NSNumber numberWithInt:20] forKey:@"ownedClowCards"];
  56.    
  57.     shaoran = [[Character alloc] init];
  58.     [shaoran setValue:@"Li Shaoran" forKey:@"characterName"];
  59.     [shaoran setValue:[NSNumber numberWithInt:21] forKey:@"ownedClowCards"];
  60.    
  61.     //Done! Now we are going to fetch the values using KVC.
  62.    
  63.     NSString *mainCharacter = [sakura valueForKey:@"characterName"];
  64.     NSNumber *mainCharCards = [sakura valueForKey:@"ownedClowCards"];
  65.    
  66.     NSString *rival = [shaoran valueForKey:@"characterName"];
  67.     NSNumber *rivalCards = [shaoran valueForKey:@"ownedClowCards"];
  68.    
  69.     NSLog(@"%@ has %d Clow Cards", mainCharacter, [mainCharCards intValue]);
  70.     NSLog(@"%@ has %d Clow Cards", rival, [rivalCards intValue]);
  71.    
  72.     //---------------------------------------------------------------------
  73.     // Here begins the KVO section.
  74.    
  75.     [sakura addObserver:sakura forKeyPath:@"ownedClowCards" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil]; //Sakura is observing her own Clow Cards.
  76.    
  77.     [sakura setValue:[NSNumber numberWithInt:22] forKey:@"ownedClowCards"];
  78.     [sakura removeObserver:sakura forKeyPath:@"ownedClowCards"];
  79.    
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement