runtime 知识点
demo https://github.com/ZOYOOPlus/runtime2
//
// ViewController.m
// runtime
// Copyright © 2017年 四海八荒. All rights reserved.
//
#import "ViewController.h"
#import "MyObject.h"
#import "UIView+TYView.h"
#import <objc/runtime.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {// 1.类相关函数
// 得到类名 ViewController
NSLog(@"%s",class_getName([self class]));
// 父类类名
// This is superClass 0x10f426da0 isa指针地址
NSLog(@"This is superClass %p",class_getSuperclass([self class]));
// 是否是元类
// Is MateClass 0
NSLog(@"Is MateClass %d",class_isMetaClass([self class]));
// 实例变量大小
// 848
size_t a = class_getInstanceSize([self class]);
NSLog(@"%ld",a);
unsigned int outCount = 0;
MyObject *objtect = [[MyObject alloc] init];
Class cls = objtect.class;
//类名
NSLog(@"Class name %s",class_getName(cls));
//父类名字
NSLog(@"SuperClass name %s",class_getName(class_getSuperclass(cls)));
//是否元类
NSLog(@"Is MetaClass %@",(class_isMetaClass(cls))?@"":@"NOT");
//变量大小
NSLog(@"MyObject size %ld",class_getInstanceSize(cls));
//变量信息
Ivar string = class_getInstanceVariable(cls, "_string");
if (string != NULL) {
NSLog(@"MyObject instance messge %s",ivar_getName(string));
}
// 属性操作
objc_property_t *v = class_copyPropertyList(cls, &outCount);
for (NSInteger i = 0; i< outCount; i++) {
objc_property_t property = v[i];
NSLog(@"property is %s ",property_getName(property));
}
free(v);
//成员变量
Ivar *vars = class_copyIvarList(cls, &outCount);
for (NSInteger i =0; i< outCount; i++) {
Ivar ivar = vars[i];
NSLog(@"copyIvarList is %s is %ld",ivar_getName(ivar),i);
}
free(vars);
//方法操作
Method *methods = class_copyMethodList(cls, &outCount);
for (NSInteger i =0; i< outCount; i++) {
Method met = methods[i];
#pragma clang diagnostic ignored"-Wformat"
NSLog(@"Method is %s",method_getName(met));
}
free(methods);
// Format specifies type 'char *' but the argument has type 'SEL _Nonnull'
// 获取方法名
Method method = class_getInstanceMethod(cls,@selector(method1));
NSLog(@"Method name is %s",method_getName(method));
// 判断方法是否存在
#pragma clang diagnostic ignored"-Wundeclared-selector"
NSLog(@"Have you method %d",class_respondsToSelector(cls, @selector(method3WithArge1:arge2:)));
// 指向函数实现的指针,相当于方法的实现
IMP imp = class_getMethodImplementation(cls, @selector(method1));
imp();
//动态创建类
#pragma clang diagnostic ignored" -Wunused-variable"
// 注:运行时规定,只能在objc_allocateClassPair与objc_registerClassPair两个函数之间为类添加变量
// 1.添加一个自定义的类 类名是MySubClass
// 父类class,类名,额外空间
Class myClass = objc_allocateClassPair(objtect.class, "MySubClass", 0);
// 2.增加方法,交换方法
//注: v@: 意思是 v是void @:没有返回参数
if( class_addMethod(myClass, @selector(mysubMethod1),(IMP)mysubMethod1, "v@:")){
class_replaceMethod(myClass, @selector(method1), (IMP)mysubMethod1,"v@:");
}
/*
3.增加一个NSSsting类型属性 属性名myString
变量size sizeof(NSString)
对齐 指针类型的为log2(sizeof(NSString*))
类型 @encode(NSString*)
class_addIvar(class,变量名,变量size,对齐,类型)
*/
//添加同名属性会失败
BOOL isd = class_addIvar(myClass, "_myString", sizeof(NSString *), log(sizeof(NSString *)), @encode(NSString *));
NSLog(@"属性是否添加成功 %d",isd);
/*
特性相关编码
属性的特性字符串 以 T@encode(type) 开头, 以 V实例变量名称 结尾,中间以特性编码填充,通过property_getAttributes即可查看
特性编码 具体含义
R readonly
C copy
& retain ARC strong
N nonatomic
G(name) getter=(name)
S(name) setter=(name)
D @dynamic
W weak
P 用于垃圾回收机制
*/
//@T
objc_property_attribute_t type;
type.name = "T";
type.value = @encode(NSString *);
//copy
objc_property_attribute_t owership = {"C",""};
//nonatomic
objc_property_attribute_t oeership2 = {"N",""};
//V_属性名
objc_property_attribute_t var = {"V","_myString"};
//特性数组
objc_property_attribute_t attributes[] = {type,owership,oeership2,var};
//向类中添加名为myString的属性,属性的特性包含在attributes中
class_addProperty(myClass, "myString", attributes, 4);
unsigned int propertyCount;
objc_property_t * properties = class_copyPropertyList(myClass, &propertyCount);
for (int i = 0; i<propertyCount; i++) {
NSLog(@"属性的名称为 : %s",property_getName(properties[i]));
NSLog(@"属性的特性字符串为: %s",property_getAttributes(properties[i]));
}
//释放属性列表数组
free(properties);
//在应用中注册由objc_allocateClassPair创建的类
objc_registerClassPair(myClass);
id instance = [[myClass alloc] init];
[instance performSelector:@selector(mysubMethod1)];
[instance performSelector:@selector(method1)];
// 销毁一个类及相关联的类
// 不过需要注意的是,如果程序运行中还存在类或其子类的实例,则不能调用针对类调用该方法
//objc_disposeClassPair(myClass);
//关联对象
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
view.backgroundColor = [UIColor redColor];
[view setTapActionWithBlock:^{
NSLog(@"saAS");
}];
[self.view addSubview:view];
// SEL
// Objective-C在编译时,会依据每一个方法的名字、参数序列,生成一个唯一的整型标识(Int类型的地址),这个标识就是SEL
// SEL只是一个指向方法的指针(准确的说,只是一个根据方法名hash化了的KEY值,能唯一代表一个方法
// 而对于字符串的比较仅仅需要比较他们的地址就可以了,可以说速度上无语伦比
// sel : 0x104dd7735
/*
三种方法来获取SEL:
sel_registerName函数
Objective-C编译器提供的@selector()
NSSelectorFromString()方法
*/
SEL sel1 = @selector(method1);
NSLog(@"sel : %p", sel1);
}
static void mysubMethod1(id self,SEL _cmd){
NSLog(@"这是添加的方法");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
增加中,可先看demo
runtime 知识点的更多相关文章
- iOS runtime 知识点整理
// ------ 动态创建类, 添加成员变量, 赋值并调用动态添加的方法 ------- @implementation ViewController - (void)viewDidLoad { [ ...
- 让你快速了解并掌握如何进行iOS开发技能
首先你要花点时间针对objective-c语言的学习:毕竟这个是iOS开发的基础(你也可以尝试用Swift,但此项目只是针对OC),编程套路其实都是差不多,多写多想多实践:关于环境的搭建就不在本文进行 ...
- 【干货分享】前端面试知识点锦集03(JavaScript篇)——附答案
三.JavaScript部分 1.谈谈你对Ajax的理解?(概念.特点.作用) AJAX全称为“Asynchronous JavaScript And XML”(异步JavaScript和XML) 是 ...
- C#知识点记录
用于记录C#知识要点. 参考:CLR via C#.C#并发编程.MSDN.百度 记录方式:读每本书,先看一遍,然后第二遍的时候,写笔记. CLR:公共语言运行时(Common Language Ru ...
- iOS runtime实用篇解决常见Crash
程序崩溃经历 其实在很早之前就想写这篇文章了,一直拖到现在. 程序崩溃经历1 平时开发测试的时候好好的,结果上线几天发现有崩溃的问题,其实责任大部分在我身上. 我的责任: 过分信赖文档,没进行容错处理 ...
- runtime 运行机制2
Mike_zh QQ:82643885 end: blogTitle 博客的标题和副标题 博客园 首页 新随笔 联系 订阅 <a id="MyLinks1_XMLLink" ...
- WIX 安装部署教程(六) 为你收集的七个知识点
前段时间整理5篇WIX(Windows Installer XML)的安装教程,但还不够完善,这里继续整理了七个知识点分享给大家.WIX最新版本3.8,点击下载 WIX安装部署(一)同MSBuild自 ...
- php易混淆知识点
一.define(“constant”, “hello world”);和const constant = “hello world”;的区别? (0).使用const使得代码简单易读,const本 ...
- Android开发涉及有点概念&相关知识点(待写)
前言,承接之前的 IOS开发涉及有点概念&相关知识点,这次归纳的是Android开发相关,好废话不说了.. 先声明下,Android开发涉及概念比IOS杂很多,可能有很多都题不到的.. 首先由 ...
随机推荐
- 当新手使用JS库遇到问题怎么办
见标题,知其意.在做网站时候,其实我们会用很多JS库,网络上流行的和公司自己封装的,这些东西都很好用,但是或多或少的有些bug或者有一些缺陷,即使真的很完善,但也可能达不到自己特定的一些需求.所以遇到 ...
- SaltStack高可用multi-master-第十三篇
multi-master官方介绍 As of Salt 0.16.0, the ability to connect minions to multiple masters has been made ...
- 在centos 6.9下Protocol Buffers数据传输及存储协议的使用(python)
我们知道Protocol Buffers是Google定义的一种跨语言.跨平台.可扩展的数据传输及存储的协议,因为将字段协议分别放在传输两端,传输数据中只包含数据本身,不需要包含字段说明,所以传输数据 ...
- codeforces 484B - LubaAndTicket - 贪心
2017-08-22 10:54:00 writer:pprp 题意如下: 给你6个数组,你的操作可以是更改某一位的数字成为0-9之间任意一个数,要求前三个数字的和与后三个数字的和相等. 问你最少用几 ...
- 在HTTP通讯过程中,是客户端还是服务端主动断开连接?
比如说:IE访问IIS,获取文件,肯定是要建立一个连接,这个连接在完成通讯后,是客户端Close了连接,还是服务端Close了连接.我用程序测模拟IE和IIS,都没有收到断开连接的消息,也就是都没有触 ...
- Gtk基础学习总结(一)
第一个GTK程序例子: #include <stdio.h> #include <gtk/gtk.h> int main(int argc, char *argv[]) { g ...
- Huffuman Coding (哈夫曼编码)
哈夫曼编码(Huffman Coding),又称霍夫曼编码,是一种编码方式,哈夫曼编码是可变字长编码(VLC)的一种.Huffman于1952年提出一种编码方法,该方法完全依据字符出现概率来构造异字头 ...
- JavaScript高级与面向对象
对象:任何事物都可以看作是对象. 1.面向对象与面向过程的概念 面向过程:凡是自己亲力亲为,自己按部就班的解决现有问题. 面向对象:自己充当一个指挥者的角色,指挥更加专业的对象帮我解决问题. 联系:面 ...
- Hive -hivevar 参数传递
命令行模式,或者说目录模式,可以使用hive 执行命令. 选项说明: -e : 执行短命令 -f : 执行文件(适合脚本封装) -S : 安静模式,不显示MR的运行过程 -hivevar : 传参数 ...
- vs2012 在调试或运行的过程中不能加断点
在使用VS2012 的过程中,突然发现在调试的过程中,不能加断点,显示断点未能绑定.在搜寻了很多解决方案后未能解决,3.23这一天,重装了VS也没有用. 便想着把网上所有的方法都试个遍也要解决这个问题 ...