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
| #import "ViewController.h" #import "AObject.h" #import <objc/runtime.h>
@implementation ViewController
- (void)viewDidLoad { [super viewDidLoad]; AObject *a1 = [[AObject alloc] init]; AObject *a2 = [[AObject alloc] init]; NSLog(@"内存:%p,%p", a1, a2); NSLog(@"类:%@,%@", a1.class, a2.class); NSLog(@"isa:%@ %@", object_getClass(a1), object_getClass(a2)); SEL sel = @selector(setP:); IMP imp1 = [a1 methodForSelector:sel]; IMP imp2 = [a2 methodForSelector:sel]; NSLog(@"IMP指针:%p %p", imp1, imp2); [a1 addObserver:self forKeyPath:@"p" options:(NSKeyValueObservingOptionNew) context:nil]; NSLog(@"添加KVO后内存:%p,%p", a1, a2); NSLog(@"添加KVO后类:%@,%@", a1.class, a2.class); NSLog(@"添加KVO后isa:%@ %@", object_getClass(a1), object_getClass(a2)); imp1 = [a1 methodForSelector:sel]; imp2 = [a2 methodForSelector:sel]; NSLog(@"添加KVO后IMP指针:%p %p", imp1, imp2); a1.p = @"1"; a2.p = @"2"; }
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context { NSLog(@"%@", change); }
@end
2019-12-06 13:56:30.457732+0800 testKVO[854:62432] 内存:0x283a0cc70,0x283a0cc90 2019-12-06 13:56:30.457797+0800 testKVO[854:62432] 类:AObject,AObject 2019-12-06 13:56:30.457821+0800 testKVO[854:62432] isa:AObject AObject 2019-12-06 13:56:30.457839+0800 testKVO[854:62432] IMP指针:0x1009e6150 0x1009e6150 2019-12-06 13:56:30.458057+0800 testKVO[854:62432] 添加KVO后内存:0x283a0cc70,0x283a0cc90 2019-12-06 13:56:30.458084+0800 testKVO[854:62432] 添加KVO后类:AObject,AObject 2019-12-06 13:56:30.458106+0800 testKVO[854:62432] 添加KVO后isa:NSKVONotifying_AObject AObject 2019-12-06 13:56:30.458124+0800 testKVO[854:62432] 添加KVO后IMP指针:0x1842b9020 0x1009e6150
|