---
title: "respondsToSelector"
author: "fantom"
date: "2013-01-15T09:31:01Z"
tags: []
categories: ["移动开发"]
slug: "respondstoselector"
draft: false
lastmod: "2013-01-15T09:31:01Z"
id: 1769933
url: "https://fantom.iteye.com/blog/1769933"
---

respondsToSelector该方法询问对象以确定其是否能够响应某个特定的消息

除了检查对象是否支持一个特定的方法，我们还可以检查类是否会创建支持一个特定的方法的对象。未来做到这一点，我们使用instancesRespondToSelector（）方法
举例：

```java
#import <Foundation/Foundation.h>

#import <stdio.h>
@interface Class1 : NSObject
{
}

-(void)print;
@end

@implementation Class1

-(void)print
{
printf("This is Class 1./n");

}

@end

int main (int argc, const char * argv[]) {
Class1 *c1=[class1 new];

//验证对象支持一个方法

if ([c1 respondstoselector: @selector(print)]==YES) {

printf("c1 has a print method. /n");

}

 

//验证类是否创建支持一个特定方法的对象

if ([class1 instancesrespondtoselector: @selector(print)]==YES) {

printf("Class1 object have a print method /n/n");

}

    return 0;

}

```

输出结果：


c1 has a print method. 

Class1 object have a print method