objective-c – 如何确定对象是否实现了特定方法?
栏目: Objective-C · 发布时间: 7年前
内容简介:翻译自:https://stackoverflow.com/questions/1103895/how-can-i-find-out-if-an-object-implements-a-particular-method
我正在迭代包含许多不同类型对象的NSArray.有很多方法可以确定对象是什么类.但是,我无法找到一个好方法来确定对象是否可以实现特定的功能.我可以将它放在try-catch中,但即使我正在捕获错误,它仍会在控制台中输出错误消息.有一个更好的方法吗?
简单的例子:
@try {
if ([element lowercaseString]) {
//do something
}
}
@catch (id theException) {
// do something else
}
respondsToSelector:
消息.提供的代码就像 if ([element respondsToSelector:@selector(lowercaseString)]) {
// ... do work
}
翻译自:https://stackoverflow.com/questions/1103895/how-can-i-find-out-if-an-object-implements-a-particular-method
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Algorithms
Sanjoy Dasgupta、Christos H. Papadimitriou、Umesh Vazirani / McGraw-Hill Education / 2006-10-16 / GBP 30.99
This text, extensively class-tested over a decade at UC Berkeley and UC San Diego, explains the fundamentals of algorithms in a story line that makes the material enjoyable and easy to digest. Emphasi......一起来看看 《Algorithms》 这本书的介绍吧!