C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了objective-c – 为什么即使在调用它之后我也无法获得我的类别实例方法?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_450_0@
我试图更好地理解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? %@",@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

我们发现实例方法是规范接口的一部分,但不是类别中的实例方法.这怎么可能发生,但它可以被成功调用?有没有办法反省并找到类别方法

解决方法

在这种情况下发生的是你假设attributionString属于NSAttributedString类型,但情况并非如此:
NSLog(@"Class: %@",[attributedString class]);

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

如果将代码中的NSAttributedString.class更改为[attributionString类],它将按预期工作.

大佬总结

以上是大佬教程为你收集整理的objective-c – 为什么即使在调用它之后我也无法获得我的类别实例方法?全部内容,希望文章能够帮你解决objective-c – 为什么即使在调用它之后我也无法获得我的类别实例方法?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。