故事背景:
這個是自己在復習KVO時,偶然想到的(就是蝦基霸寫的)
埋同,類似的面試題應該有小伙伴遇到過~
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
Person *p = [Person new];
[p setValue:@"aa" forKey:@"name"];
[p addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil];
Method m = class_getInstanceMethod([self class], @selector(log));
const char *className = object_getClassName(p);
NSString *name = [[NSString alloc] initWithUTF8String:className];
class_addMethod(NSClassFromString(name), method_getName(m), method_getImplementation(m), method_getTypeEncoding(m));
[p performSelector:@selector(log)];
}
- (void)log
{
NSLog(@"self: %s", object_getClassName(self));
}
@end
代碼如上
問題:方法log
會打印什么?
PS:該題涉及到KVO原理州叠,消息發(fā)送原理以及類結構中的ISA指針三個知識點