objective-c – 如何确定对象是否实现了特定方法?

栏目: Objective-C · 发布时间: 5年前

内容简介:翻译自: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


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

查看所有标签

猜你喜欢:

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

Sass and Compass in Action

Sass and Compass in Action

Wynn Netherland、Nathan Weizenbaum、Chris Eppstein、Brandon Mathis / Manning Publications / 2013-8-2 / USD 44.99

Written by Sass and Compass creators * Complete Sass language reference * Covers prominent Compass community plug-ins * Innovative approach to creating stylesheets Cascading Style Sheets paint the we......一起来看看 《Sass and Compass in Action》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具