Objective-C响应方法
2015-08-04 17:33
513 查看
响应方法
响应方法
[对象 performSelector:(SEL)];
响应方法
[对象 performSelector:(SEL)];
#import <Foundation/Foundation.h> //动物类 @interface Animal : NSObject { } -(void)run; @end @implementation Animal -(void)run{ NSLog(@"动物在跑!"); } @end //狗类 @interface Dog : NSObject { } -(void)run; -(void)eat; -(void)eat:(NSString*)_foodName; -(void)eat:(NSString*)_foodName andDogName :(NSString*)_dogName; @end @implementation Dog -(void)run{ NSLog(@"狗在跑!"); } -(void)eat{ NSLog(@"狗在吃"); } -(void)eat:(NSString*)_foodName{ NSLog(@"狗在吃:%@", _foodName); } -(void)eat:(NSString*)_foodName andDogName :(NSString*)_dogName{ NSLog(@"这是%@狗,吃的食物是:%@",_dogName,_foodName); } @end //以下是动态类型检测 int main(int argc,const char * argv[]){ Animal *animal = [Dog new]; SEL s1 = @selector(eat); if([animal responseToSelector:s1]){ //这个performSelector就是响应方法 是无参的方法 [animal performSelector:s1]; } SEL s2 = @selector(eat:); if([animal respondsToSelector:s2]){ //这个是响应有参数的方法 //不能这么写,因为要传一个参数 //[animal performSelector:s2]; [animal performSelector:s2 withObject:@"有参数的响应方法"]; } SEL s3 = @select(eat:andDogName:); if([animal respondsToSelector:s3]){ //以下是响应一个方法,传递两个参数 [animal performSelector:s3 withObject:@"参数1" withObject:@"参数2"]; } return 0; }
相关文章推荐
- Object-C内存管理
- Objective-C 动态类型检测 类和实力对象能否响应方法
- OC基础回顾(八)内存管理
- GObject入门教程
- Objective-C 的动态类型判断
- Objective C 学习笔记
- JSONObject的getString()和optString()的区别
- *** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from o
- Objective-C学习- 字符串类型 NSString 和 NSMutableString
- Objective-C学习- 数字对象 NSNumber
- Objective-C学习-数组 NSArray 和 NSMutableArray
- Objective-C学习- 便利初始化函数和便利构造器
- Object-c 典型的单例写法
- Objective-C ARC单例模式
- Objective-C学习笔记_内存管理(二)
- objective-c面向对象
- Objective-C学习笔记_内存管理(一)
- 【iOS开发之Objective-C】协议和代理
- Objective-C 多参数成员函数【好文收藏】
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法