iOS笔记-performSelector的探索
官方文档介绍
- (id)performSelector:(SEL)aSelector;
Description
Sends a specified message to the receiver and returns the result of
the message. The performSelector: method is equivalent to sending an
aSelector message directly to the receiver. For example, the following
messages all do the same thing:
Listing 1
id aClone = [anObject copy];
id aClone = [anObject performSelector:@selector(copy)];
id aClone = [anObject performSelector:sel_getUid("copy")];
The performSelector: method allows you to send messages that aren’t
determined until run-time. This means that you can pass a variable
selector as the argument:
Listing 2
SEL aSelector = findTheAppropriateSelectorForTheCurrentSituation();
id returnedObject = [anObject performSelector:aSelector];
核心信息:
1、执行效果和发送消息等价
2、performSelector允许发送未在运行时确定的消息
测试
ViewController.h
@interface ViewController : UIViewController@property (nonatomic, copy) NSString *birthday; // 支持@property (nonatomic, weak) UIColor *color; // 支持@property (nonatomic, assign) NSInteger age; // 不支持-(NSInteger)test; // 支持
@end
ViewController.m
@interface ViewController ()@property (nonatomic, copy) NSString *name;- (void)test2;
@end@implementation ViewController
- (void)viewDidLoad {[super viewDidLoad];self.birthday = @"1212";self.name = @"zw";self.color = [UIColor redColor];self.age = 121;TestAViewController *testAVC = [TestAViewController new];testAVC.vc = self;[self.navigationController pushViewController:testAVC animated:YES];
}
- (NSInteger)test {NSLog(@"dd");return 11;
}- (void)test2 {NSLog(@"dd2");
}
@end
TestAViewController.m
// 对象是否实现了改方法if ([_vc respondsToSelector:@selector(birthday)]) {// performSelector获取的不能是基本数据类型变量, 方法不支持返回值,可以获取到对象中的私有属性值id birthdayValue = [_vc performSelector:@selector(birthday)];NSLog(@"%@",birthdayValue);}if ([_vc respondsToSelector:@selector(name)]) {// performSelector获取的不能是基本数据类型变量, 方法不支持返回值,可以获取到对象中的私有属性值id nameValue = [_vc performSelector:@selector(name)];NSLog(@"%@",nameValue);}if ([_vc respondsToSelector:@selector(color)]) {// performSelector获取的不能是基本数据类型变量, 方法不支持返回值,可以获取到对象中的私有属性值id colorValue = [_vc performSelector:@selector(color)];NSLog(@"%@",colorValue);}ViewController *vc = (ViewController*)_vc;NSLog(@"%@",vc.birthday); // 效果和performSelector获取到到值一样NSLog(@"%@",[vc test]); // 效果和performSelector一样if ([vc respondsToSelector:@selector(test)]) {// performSelector获取的不能是基本数据类型变量, 方法不支持返回值,可以获取到对象中的私有属性值[vc performSelector:@selector(test)];}if ([vc respondsToSelector:@selector(test2)]) {// performSelector获取的不能是基本数据类型变量, 方法不支持返回值,可以获取到对象中的私有属性值[vc performSelector:@selector(test2)];}
结论
- 对于公开属性和方法,通过对象使用performSelector和直接调用结果没有区别
- performSelector可以获取到及调用对象中的私有属性和方法
- performSelector获取变量时不能是基本数据类型的数据,copy、strong、weak修饰的都可以
- performSelector在调用方法时不支持返回值,否则会报错
- performSelector在编译过程中不存在对应方法不会报错,只有运行时才会报错,直接调用编译时就会报错, 为了避免运行报错,performSelector需要使用respondsToSelector检查方法或者属性是否存在
iOS笔记-performSelector的探索相关推荐
- IOS笔记CALayer的position和anchorPoint
IOS笔记CALayer的position和anchorPoint CALayer有2个非常重要的属性:position和anchorPoint @property CGPoint position; ...
- ROS学习笔记二:探索ROS文件系统
ROS学习笔记二:探索ROS文件系统 ROS针对自己文件的特性,具有一些自己的工具命令,当针对ROS文件进行操作的时候是非常方便的.这些命令和ubuntu系统原有的命令相似但却不同,单独针对ROS文件 ...
- iOS图像渲染 + 动画探索
图像渲染过程 在介绍渲染过程之前先介绍一下VSync信号 VSync 信号由硬件时钟生成,每秒钟发出 60 次(这个值取决设备硬件,比如 iPhone 真机上通常是 59.97).iOS 图形服务接收 ...
- 58同城iOS客户端Hybrid框架探索
作者:杜艳新,刘文军.58同城iOS高级研发工程师,专注于App Hybrid框架的架构研发,主导了58同城App的Hybird混合研发的系统架构以及研发. 责编:唐小引,欢迎技术投稿.约稿.给文章纠 ...
- 58 同城 iOS 客户端 Hybrid 框架探索
[CSDN 编者按]58 同城 iOS 客户端的 Hybrid 框架在最初设计和演进的过程中,遇到了许多问题.为此,整个 Hybrid 框架产生了很大的变化.本文作者将遇到的典型问题进行了总结,并重点 ...
- IOS 笔记大全 (UI控件到网络协议)
1 MVC模式:(model+view+controller):是一种帮你把代码功能和显示划分出来的设计模式: 2 model:较为底层的数据引擎,负责管理实体中所继承的数据: 3 view:和用户交 ...
- iOS笔记059 - 网络总结
网络 基本概念 客户端:client 服务器:server 请求:request 响应:response 过程 客户端 -> 发送请求 -> 服务器(连接数据库) 服务器 -> 发送 ...
- iOS 笔记 【经典】
MVC模式:(model+view+controller):是一种帮你把代码功能和显示划分出来的设计模式: model:较为底层的数据引擎,负责管理实体中所继承的数据: view:和用户交互界面: c ...
- IOS逆向--performSelector动态调用
performSelector和直接调用方法的区别 performSelector: withObject:是在iOS中的一种方法调用方式.他可以向一个对象传递任何消息,而不需要在编译的时候声明这些方 ...
最新文章
- pycharm设置环境变量和参数
- 200kb以上图片储存
- 软考中高项学员:2016年3月14日作业
- CSS中的a标签几个访问状态记录
- safari里的touch事件解析
- java 打印gc_java – 以编程方式打印启用GC日志记录时通常在JVM出口上打印的堆使用情况...
- linux 路由表(转)
- JavaScript学习笔记之数组(一)
- 使用express重构博客项目
- Atitit 互联网2018风口与趋势大盘点 目录 1.1. 短视频	1 1.2. 互联网金融	1 1.3. 区块链	1 1.4. 新零售	1 2. 2018热度榜丨你能抓住的风口有几个?.html
- MySQL可视化工具连不上或mysql启动不了
- USB协议学习笔记 - 引言
- 接口性能测试方案分析
- 地摊经济:78岁高龄老人摆地摊的背后,蕴藏着9000万老年再就业市场的巨大商机
- 光纤耦合系统的公差分析
- IOS5短信拦截怎么实现?
- Murata村田高压电容的国产替代--赫威斯电容(HVC Capacitor)
- 广告主流量主怎么申请(微信)
- 【机器学习】多项式回归
- 熵、条件熵、联合熵、互信息的理解
热门文章
- 用python画樱桃小丸子_手机插画| 可爱的樱桃小丸子绘画全过程,你也可以画出来!...
- GNSS速度场简易MATLAB克里金插值
- c语言 虚拟摄像头设备_Windows下虚拟UVC摄像头开发演示
- 液压比例溢流阀R901170278、DBET-62/200G24-8K4V
- ftp客户端android版,Primitive FTPd(FTP客户端)
- 微信小程序基础入门(一):小程序界面介绍创造自己的第一个小程序
- ipad做win10扩展屏的方法
- 说说看板在项目中的应用
- MegaFace 人脸数据集 戴眼镜数据集
- 网站SEO关键词排名优化经验分享-建站后的推广细节