5

私は、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

標準インターフェースの一部であるインスタンス メソッドが見つかりますが、カテゴリのインスタンス メソッドは見つかりません。これがどのように発生し、正常に呼び出されるのでしょうか? 内省してカテゴリメソッドを見つける方法はありますか?

4

1 に答える 1

3

この場合に起こることはattributedString、タイプNSAttributedStringであると仮定することですが、そうではありません。

NSLog(@"Class: %@", [attributedString class]);

// Produces this output
2013-04-19 19:50:21.955 TestApp[1611:303] Class: NSConcreteAttributedString

NSAttributedString.classコードを変更する[attributedString class]と、期待どおりに動作します。

于 2013-04-19T17:53:42.663 に答える