(译)KVO的内部实现
- // gcc -o kvoexplorer -framework Foundation kvoexplorer.m
- #import <Foundation/Foundation.h>
- #import <objc/runtime.h>
- @interface TestClass : NSObject
- {
- int x;
- int y;
- int z;
- }
- @property int x;
- @property int y;
- @property int z;
- @end
- @implementation TestClass
- @synthesize x, y, z;
- @end
- static NSArray *ClassMethodNames(Class c)
- {
- NSMutableArray *array = [NSMutableArray array];
- unsigned int methodCount = 0;
- Method *methodList = class_copyMethodList(c, &methodCount);
- unsigned int i;
- for(i = 0; i < methodCount; i++)
- [array addObject: NSStringFromSelector(method_getName(methodList[i]))];
- free(methodList);
- return array;
- }
- static void PrintDescription(NSString *name, id obj)
- {
- NSString *str = [NSString stringWithFormat:
- @"%@: %@\n\tNSObject class %s\n\tlibobjc class %s\n\timplements methods <%@>",
- name,
- obj,
- class_getName([obj class]),
- class_getName(obj->isa),
- [ClassMethodNames(obj->isa) componentsJoinedByString:@", "]];
- printf("%s\n", [str UTF8String]);
- }
- int main(int argc, char **argv)
- {
- [NSAutoreleasePool new];
- TestClass *x = [[TestClass alloc] init];
- TestClass *y = [[TestClass alloc] init];
- TestClass *xy = [[TestClass alloc] init];
- TestClass *control = [[TestClass alloc] init];
- [x addObserver:x forKeyPath:@"x" options:0 context:NULL];
- [xy addObserver:xy forKeyPath:@"x" options:0 context:NULL];
- [y addObserver:y forKeyPath:@"y" options:0 context:NULL];
- [xy addObserver:xy forKeyPath:@"y" options:0 context:NULL];
- PrintDescription(@"control", control);
- PrintDescription(@"x", x);
- PrintDescription(@"y", y);
- PrintDescription(@"xy", xy);
- printf("Using NSObject methods, normal setX: is %p, overridden setX: is %p\n",
- [control methodForSelector:@selector(setX:)],
- [x methodForSelector:@selector(setX:)]);
- printf("Using libobjc functions, normal setX: is %p, overridden setX: is %p\n",
- method_getImplementation(class_getInstanceMethod(object_getClass(control),
- @selector(setX:))),
- method_getImplementation(class_getInstanceMethod(object_getClass(x),
- @selector(setX:))));
- return 0;
- }
- control: <TestClass: 0x104b20>
- NSObject class TestClass
- libobjc class TestClass
- implements methods <setX:, x, setY:, y, setZ:, z>
- x: <TestClass: 0x103280>
- NSObject class TestClass
- libobjc class NSKVONotifying_TestClass
- implements methods <setY:, setX:, class, dealloc, _isKVOA>
- y: <TestClass: 0x104b00>
- NSObject class TestClass
- libobjc class NSKVONotifying_TestClass
- implements methods <setY:, setX:, class, dealloc, _isKVOA>
- xy: <TestClass: 0x104b10>
- NSObject class TestClass
- libobjc class NSKVONotifying_TestClass
- implements methods <setY:, setX:, class, dealloc, _isKVOA>
- Using NSObject methods, normal setX: is 0x195e, overridden setX: is 0x195e
- Using libobjc functions, normal setX: is 0x195e, overridden setX: is 0x96a1a550
- (gdb) print (IMP)0x96a1a550
- $1 = (IMP) 0x96a1a550 <_NSSetIntValueAndNotify>
- 0013df80 t __NSSetBoolValueAndNotify
- 000a0480 t __NSSetCharValueAndNotify
- 0013e120 t __NSSetDoubleValueAndNotify
- 0013e1f0 t __NSSetFloatValueAndNotify
- 000e3550 t __NSSetIntValueAndNotify
- 0013e390 t __NSSetLongLongValueAndNotify
- 0013e2c0 t __NSSetLongValueAndNotify
- 00089df0 t __NSSetObjectValueAndNotify
- 0013e6f0 t __NSSetPointValueAndNotify
- 0013e7d0 t __NSSetRangeValueAndNotify
- 0013e8b0 t __NSSetRectValueAndNotify
- 0013e550 t __NSSetShortValueAndNotify
- 0008ab20 t __NSSetSizeValueAndNotify
- 0013e050 t __NSSetUnsignedCharValueAndNotify
- 0009fcd0 t __NSSetUnsignedIntValueAndNotify
- 0013e470 t __NSSetUnsignedLongLongValueAndNotify
- 0009fc00 t __NSSetUnsignedLongValueAndNotify
- 0013e620 t __NSSetUnsignedShortValueAndNotify
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。