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
}
如建议的那样,您可以使用在NSObject上声明的 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


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

An Introduction to the Analysis of Algorithms

An Introduction to the Analysis of Algorithms

Robert Sedgewick、Philippe Flajolet / Addison-Wesley Professional / 1995-12-10 / CAD 67.99

This book is a thorough overview of the primary techniques and models used in the mathematical analysis of algorithms. The first half of the book draws upon classical mathematical material from discre......一起来看看 《An Introduction to the Analysis of Algorithms》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

MD5 加密
MD5 加密

MD5 加密工具

html转js在线工具
html转js在线工具

html转js在线工具