OC-033.SEL类型
来源:互联网 发布:遥控软件哪个最好 编辑:程序博客网 时间:2024/06/06 12:21
SEL是一种数据类型,代表方法签名,通过它就找到这个对象上方法
SEL和id都是一种结构体类型指针
1、定义变量 SEL 变量名称
2、通过@selector(方法名称) 快速生成一个SEL,(selector:选择器的意思,选择使用哪个方法)
3、可以作为方法的实参
4、可以作为方法的形参
5、可以作为方法返回值
SEL和id都是一种结构体类型指针
1、定义变量 SEL 变量名称
2、通过@selector(方法名称) 快速生成一个SEL,(selector:选择器的意思,选择使用哪个方法)
3、可以作为方法的实参
4、可以作为方法的形参
5、可以作为方法返回值
使用场景:可以用来做一个通知,通知某个对象去执行某个方法,在方法内部请一定要判断一下是否响应
涉及到的方法:
1.[... performSelector:@selector()]; 让某对象执行某方法
2.[... respondToSelector:@selector()];返回BOOL类型,判断对象是否响应了方法
#import <Foundation/Foundation.h>//------main#import "LSPerson.h"#import "LSCar.h"#import "LSButton.h"int main(int argc, const char * argv[]) { @autoreleasepool { SEL sel1 = @selector(say); SEL sel2 = @selector(show:);//注意不要忘记加: LSPerson *p = [[LSPerson alloc] init]; if([p respondsToSelector:sel1]){ //respondsToSelector方法返回bool,表示对象能否响应后面的方法,如何可以,那么就执行。 [p performSelector:sel1];//这里的sel1是作为实参 //performSelector这个方法是让对象执行某个方法 } [p performSelector:sel2]; LSCar *car = [[LSCar alloc] init]; [p orderCar:car performSelector:@selector(show:)];//这里的sel是作为形参 LSButton *button = [[LSButton alloc] init]; button.sel = @selector(start); button.obj = car; [button click]; } return 0;}
#import <Foundation/Foundation.h>//------LSPerson.h@interface LSPerson : NSObject@property (nonatomic,assign)int age;-(void) say;-(void) show:(int) age;-(void) orderCar:(id) car performSelector:(SEL) sel;//这里的sel是作为形参@end
#import "LSPerson.h"//------LSPerson.m@implementation LSPerson-(void) say{ NSLog(@"%s",__func__);}-(void) show:(int) age{ NSLog(@"age = %d",_age);}-(void) orderCar:(id) car performSelector:(SEL) sel{ if ([car respondsToSelector:sel]) { //判断id对象没是否有这个方法 NSLog(@"car内有这个方法"); [car performSelector:sel]; }else{ NSLog(@"car内没有这个方法"); }}@end
#import <Foundation/Foundation.h>//------LSCar.h@interface LSCar : NSObject-(void) start;@end
#import "LSCar.h"//------LSCar.m@implementation LSCar-(void) start{ NSLog(@"car发动了");}@end
#import <Foundation/Foundation.h>//------LSButton.h@interface LSButton : NSObject@property(nonatomic,strong)id obj;@property(nonatomic,assign)SEL sel;-(void) click;@end
#import "LSButton.h"//------LSButton.m@implementation LSButton-(void) click{ if ([self.obj respondsToSelector:self.sel]) {//判断是否响应 [self.obj performSelector:self.sel]; }}@end
0 0
- OC-033.SEL类型
- OC-SEL类型
- OC语言--SEL类型小记
- OC - SEL类型的使用
- OC基础—description方法、SEL类型
- OC学习笔记SEL类型初步理解
- OC SEL
- OC学习:SEL类型数据和selector的用法
- 黑马程序员-OC语言-SEL类型(听课笔记)
- SEL类型
- SEL类型
- SEL类型
- SEL 类型
- SEL类型
- SEL类型
- SEL类型
- OC语言--SEL
- oc中的选择器SEL
- bzoj 1208 [HNOI2004]宠物收养所 Splay
- Objective-C浅谈面向对象
- CABasicAnimation的基本使用方法(移动·旋转·放大·缩小)
- MVC特性---轻松实现前后台数据的传递
- Android应用安装错误:INSTALL_FAILED_MEDIA_UNAVAILABLE
- OC-033.SEL类型
- linux文件系统
- tar解压文件时出错-This does not look like a tar archive
- 剑指offer系列之三十五:两个链表的第一个公共节点
- linux文件系统
- Brocade交换机和Cisco交换机RPVST互操作
- ScrollView嵌套listview
- Could not find gem 'sass-rails (~> 5.0) x64-mingw32' in any of the gem sources listed in your Gemfil
- CABasicAnimation学习Demo 包括了一些常用的动画效果