Guest User

Untitled

a guest
Nov 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. //
  2. // main.m
  3. // KVOTest
  4. //
  5. // Created by Ilya Kulakov on 04.07.12.
  6. // Copyright (c) 2012 kulakov.ilya@gmail.com. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11.  
  12. @interface ObjectWithArray : NSObject
  13. @property (copy) NSArray *array;
  14. @end
  15.  
  16.  
  17. @interface TopObject : NSObject
  18. @property (retain) ObjectWithArray *property;
  19. @end
  20.  
  21.  
  22. @interface Observer : NSObject
  23. @end
  24.  
  25.  
  26. #pragma mark -
  27.  
  28.  
  29. @implementation ObjectWithArray
  30. @synthesize array;
  31. @end
  32.  
  33.  
  34. @implementation TopObject
  35. @synthesize property;
  36. @end
  37.  
  38.  
  39. @implementation Observer
  40.  
  41. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  42. {
  43. NSLog(@"Path: %@", keyPath);
  44. NSLog(@"Change: %@", change);
  45. }
  46.  
  47. @end
  48.  
  49.  
  50. #pragma mark -
  51.  
  52.  
  53. int main(int argc, const char * argv[])
  54. {
  55. @autoreleasepool {
  56. ObjectWithArray *o1 = [[ObjectWithArray new] autorelease];
  57. TopObject *o2 = [[TopObject new] autorelease];
  58. Observer *o3 = [[Observer new] autorelease];
  59.  
  60. [o2 addObserver:o3 forKeyPath:@"property.array" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial context:NULL];
  61. o2.property = o1;
  62. o1.array = [NSArray arrayWithObject:@"mamba"];
  63. }
  64. return 0;
  65. }
Add Comment
Please, Sign In to add comment