我试图更好地理解 Objective-C 运行时。考虑NSAttributedString.h
哪个具有最小的界面,然后是更广泛的类别NSExtendedAttributedString
。
现在考虑以下代码:
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithAttributedString:[[NSAttributedString alloc] initWithString:@"ABC"]];
NSLog(@"Result: %@", attributedString);
NSLog(@"Exists? %@", [NSString stringWithUTF8String:sel_getName(method_getName(class_getInstanceMethod(NSAttributedString.class, @selector(initWithAttributedString:))))]);
NSLog(@"Exists? %@", [NSString stringWithUTF8String:sel_getName(method_getName(class_getInstanceMethod(NSAttributedString.class, @selector(string))))]);
// Produces this output
2013-04-19 10:17:35.364 TestApp[53300:c07] Result: ABC{
}
2013-04-19 10:17:35.364 TestApp[53300:c07] Exists? <null selector>
2013-04-19 10:17:35.365 TestApp[53300:c07] Exists? string
我们发现实例方法是规范接口的一部分,但不是该类别中的实例方法。这怎么会发生,但它可以被成功调用?有没有办法自省并找到类别方法?