Runtime实战
为了学习Runtime,我们应该需要从几个方面入手:
- 是什么?
- 为什么会出现?
- 怎么做?
- 分享
- 能干嘛?
是什么? 为什么会出现?
C语言是静态语言,决定阶段是在编译期,而我们伟大的Apple工程师(其实Objective-C不是Apple发明的,这里姑且这么算吧)把Objective-C定义为了动态语言。那要让Objectiive-C具有动态语言的特性,就必须有一个东西承载这种特性,而这种特性就是Runtime。个人理解,如有偏颇见谅!
具体其他定义可Google搜索,会看到有很多资料,不多说了。
怎么做?
在Objective-C中一个类其实主要分几个部分:
- protocol
- Ivar
- property
- method
今天我们就来研究一下这主要的几个部分。
先上一个随便写的类:
#import <Foundation/Foundation.h>
@protocol RuntimeBaseProtocol <NSObject>
@optional
- (void)doBaseAction;
@end
@protocol RuntimeProtocol <NSObject>
@required
- (void)doRequiredAction;
@optional
- (void)doOptionalAction;
@end
@interface RuntimeObject : NSObject <RuntimeProtocol,RuntimeBaseProtocol>
{
NSString *name;
NSString *kind;
}
@property (nonatomic, strong) NSString *value;
@property (nonatomic, assign) int age;
+ (void)doClassMethod;
@end
简答解释一下,我们定义了一个叫RuntimeObject的类,类里面定义了一些东西:
- 实现了RuntimeProtocol和RuntimeBaseProtocol协议
- name、kind两个实例变量
- value、age两个property
- doClassMethod函数
接下来就是我们今天真正的主题,如何通过Runtime动态性的获取到这些东西?
其一,我们先Get下Protocol:
unsigned int count;
__unsafe_unretained Protocol **protocols = class_copyProtocolList([RuntimeObject class], &count);
for (unsigned int i = 0; i < count; i++) {
const char *name = protocol_getName(protocols[i]);
printf("%s\n",name);
}
我们发现真的准确的输出了呃!看:
RuntimeProtocol
RuntimeBaseProtocol
timg.jpeg
其二,我们再来试试Get下Property:
unsigned int count;
objc_property_t *propertys = class_copyPropertyList([RuntimeObject class], &count);
for (unsigned int i = 0; i < count; i++) {
const char *name = property_getName(propertys[i]);
printf("%s\n",name);
}
输出如下:
value
age
hash
superclass
description
debugDescription
提示:hash、superclass、description、debugDescription是从NSObject来的
我们再一次
timg.jpeg
接下来我们Get下Ivar、Method均告完美,哈哈哈!!!
分享
其实我们往上翻翻代码不难看出,Runtime给我们提供了一组函数,这些函数的形式主要为class_copyXXXX。这组函数就是用来获取Ivar、Method、Property、Protocol的,他们分别是:
- class_copyIvarList
- class_copyPropertyList
- class_copyProtocolList
- class_copyMethodList
能干嘛?
如果你看到了这些,首先我先感谢你的支持!
👍 但是我们还是得要讨论下能用来干嘛呢?有什么鸟用?上面所有的代码都是在我自己写的class基础之上测试的,我都已经有我写的代码,何必多此一举去获取Method、Property等等呐?
骚年,不急!我们假设哈,如果某天你的主管要求你把他手机上所有安装过的手机的Bundle ID提取出来,然后用Excel表统计给他,你怎么搞?你是不是想拿榔头砸他-去你Y的,什么JB玩意。但是骚年,我们不要忘记了,我们是coder啊!我们难道就没有办法吗?
直接上解决方法:
Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
NSObject *workspace = [LSApplicationWorkspace_class performSelector:@selector(defaultWorkspace)];
NSLog(@"Installed apps:%@",[workspace performSelector:@selector(allApplications)]);
#pragma clang diagnostic pop
你调试一下就知道了,这段代码可以提取到所有已经安装过的App列表,包含Bundle ID喔!它就是通过Runtime去获取workspace类,然后performSelector函数!
后记
好了,Runtime先到这吧。其实它很简单,也就是Apple提供了一堆函数给你,然后让你可以操作Ivar、Protocol、Property、Method,也就仅此而已!
附带:
- 一张截图
就截这么多吧,剩下的都一样
:
- 通过Class获取链接库名称
Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
const char *name = class_getImageName(LSApplicationWorkspace_class);
printf("%s\n",name);
- 记录Objective-C中所有消息(也是因为Runtime,新版本SDK已无法直接调用那个函数,但是可通过extern骗过编译器!)
http://blog.csdn.net/justinjing0612/article/details/9053961
落幕
所有测试代码如下简书也不能上传附件,不上GitHub了,太简单了
:
{ // Ivar
unsigned int count;
Ivar *ivars = class_copyIvarList([RuntimeObject class], &count);
for (unsigned int i = 0; i < count; i++) {
Ivar ivar = ivars[i];
const char *name = ivar_getName(ivar);
printf("%s\n",name);
}
}
printf("\n\n\n");
{ // property
unsigned int count;
objc_property_t *propertys = class_copyPropertyList([RuntimeObject class], &count);
for (unsigned int i = 0; i < count; i++) {
const char *name = property_getName(propertys[i]);
printf("%s\n",name);
}
}
printf("\n\n\n");
{ // protocol
unsigned int count;
__unsafe_unretained Protocol **protocols = class_copyProtocolList([RuntimeObject class], &count);
for (unsigned int i = 0; i < count; i++) {
const char *name = protocol_getName(protocols[i]);
printf("%s\n",name);
}
}
printf("\n\n\n");
{ // method
unsigned int count;
Method *methods = class_copyMethodList([RuntimeObject class], &count);
for (unsigned int i = 0; i < count; i++) {
SEL sel = method_getName(methods[i]);
printf("%s\n",sel_getName(sel));
}
}
printf("\n\n\n");
{ // Get dynamic framework name
Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
const char *name = class_getImageName(LSApplicationWorkspace_class);
printf("%s\n",name);
}
printf("\n\n\n");
{ // Installed apps
Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
NSObject *workspace = [LSApplicationWorkspace_class performSelector:@selector(defaultWorkspace)];
NSLog(@"Installed apps:%@",[workspace performSelector:@selector(allApplications)]);
#pragma clang diagnostic pop
}