SEL的一些小事情(随便写写)
来源:互联网 发布:买房需要注意什么 知乎 编辑:程序博客网 时间:2024/06/16 08:17
SEL是对方法的一个包装 将方法包装成一个SEL类型的数据,根据SEL类型的数据,寻找方法的地址 找到地址后就可以调用方法
{
_cmd:每个方法内部都有这样一个指令,类型是SEL,这表明_cmd是当前的方法
NSStringFromSelector(A selector):根据selector返回一个字符串(方法名字)
}
通过以上叙述,我们可以在方法中这样写
//// SELTest.h// OCStudy//// Created by LiuMingchuan on 15/9/25.// Copyright © 2015年 LMC. All rights reserved.//#import <Foundation/Foundation.h>@interface SELTest : NSObject- (void)test001:(NSString *)str;@end
//// SELTest.m// OCStudy//// Created by LiuMingchuan on 15/9/25.// Copyright © 2015年 LMC. All rights reserved.//#import "SELTest.h"@implementation SELTest- (void)test001:(NSString *)str { NSLog(@"Function :%@ outPut:%@", NSStringFromSelector(_cmd),str);}@end
SELTest *sel = [[SELTest alloc]init];[sel test001:@"这是输出内容"];输出:
2015-09-25 23:58:41.406 OCStudy[4564:2058675] Function :test001: outPut:这是输出内容我们看到输出的正式方法的名字(注意后面的冒号)
如果我们知道方法的名字,同样我们可以做成一个SEL类型的数据
NSString *selStr = @"test001:";SEL selTest = NSSelectorFromString(selStr);[sel performSelector:selTest withObject:@"这是通过字符串生成SEL的调用"];输出:
2015-09-26 00:16:58.261 OCStudy[4608:2130842] Function :test001: outPut:这是通过字符串生成SEL的调用问题:
上边的绿色部分会有警告
performSelector may cause a leak because its selector is unknown
在ARC项目中使用
主要是警告信息,在非ARC项目中没有这个警告。如果是在某一处修改只需要加入下列代码:
#pragma clang diagnostic push#pragma clang diagnostic ignored "-Warc-performSelector-leaks" [self.ticketTarget performSelector: self.ticketAction withObject: self];//此处是你调用函数的地方#pragma clang diagnostic pop
#define SuppressPerformSelectorLeakWarning(Stuff) \_Pragma("clang diagnostic push") \_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \Stuff; \_Pragma("clang diagnostic pop") \
如果没有返回结果,可以直接按如下方式调用:SuppressPerformSelectorLeakWarning( [_target performSelector:_action withObject:self]);
如果要返回结果,可以按如下方式调用:id result;SuppressPerformSelectorLeakWarning( result = [_target performSelector:_action withObject:self]);
SuppressPerformSelectorLeakWarning([sel performSelector:selTest withObject:@"这是通过字符串生成SEL的调用"]);输出:
2015-09-26 00:55:13.765 OCStudy[4677:2357874] Function :test001: outPut:这是通过字符串生成SEL的调用
0 0
- SEL的一些小事情(随便写写)
- DEBUG时的一些小技巧(随便写写)
- 随便的写写的
- 随便写写《他的国》(3)
- 随便写写的java笔记(9)
- 随便写写今天一些知识点(二).
- 大四的秋天,随便写写
- 我的mysql,随便写写
- 随便写写1 (08.3)
- 野指针(随便写写)
- 随便写写不一定对 - 小问题总结
- 随便写写今天一些知识点.
- 随便写写的java笔记(9)(1)
- 随便写写的java笔记(9)(2)
- 最近看了一些东西,随便写写JFinal的一些东西吧
- 随便写写......
- 随便写写
- 随便写写
- Android 之SharedPreference使用
- 华为故事,李一男,任正非
- MD5加密算法
- Kefa and Park
- CollectionView
- SEL的一些小事情(随便写写)
- Material Design风格的下拉刷新
- UVa 12531 - Hours and Minutes
- HDU 5445 Food Problem、UVa 10163 Storage Keepers、POJ 3260 The Fewest Coins(两次dp)
- Lucene 的使用
- Java 权限修饰符
- 2015华为校招机试题
- 算法导论习题,思考题题解博主录
- JAVA学习第四天