ImageVerifierCode 换一换
格式:DOCX , 页数:26 ,大小:36.54KB ,
资源ID:8897959      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-8897959.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(iOS Objc Runtime 教程+实例Demo.docx)为本站会员(b****6)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

iOS Objc Runtime 教程+实例Demo.docx

1、iOS Objc Runtime 教程+实例DemoiOS Objc Runtime 教程+实例Demo应用场景将某些OC代码转为运行时代码,探究底层,比如block的实现原理拦截系统自带的方法调用(Swizzle 黑魔法),比如拦截imageNamed:、viewDidLoad、alloc实现分类也可以增加属性实现NSCoding的自动归档和自动解档实现字典和模型的自动转换。(MJExtension)修BUG神器,如果大型框架的BUG 通过Runtime来解决,非常好用。一些常用类型MethodMethod An opaque type that represents a method in

2、 a class definition. Declaration typedef struct objc_method *Method;代表类定义中的方法的不透明类型。ClassClass An opaque type that represents an Objective-C class. Declaration typedef struct objc_class *Class;代表Objective-C中的类IvarAn opaque type that represents an instance variable. Declaration typedef struct objc_iv

3、ar *Ivar;代表实例变量IMPIMP A pointer to the start of a method implementation.指向方法实现的开始的内存地址的指针。SELSEL Defines an opaque type that represents a method selector. Declaration typedef struct objc_selector *SEL;代表方法的选择器类与对象操作函数runtime有很多的函数可以操作类和对象。类相关的是class为前缀,对象相关操作是objc或object_为前缀。类相关操作函数name/ 获取类的类名const

4、 char * class_getName ( Class cls );super_class和meta-class/ 获取类的父类Class class_getSuperclass ( Class cls );/ 判断给定的Class是否是一个meta classBOOL class_isMetaClass ( Class cls );instance_size/ 获取实例大小size_t class_getInstanceSize ( Class cls );成员变量(ivars)及属性/成员变量操作函数/ 获取类中指定名称实例成员变量的信息Ivar class_getInstanceVa

5、riable ( Class cls, const char *name );/ 获取类成员变量的信息Ivar class_getClassVariable ( Class cls, const char *name );/ 添加成员变量BOOL class_addIvar ( Class cls, const char *name, size_t size, uint8_t alignment, const char *types ); /这个只能够向在runtime时创建的类添加成员变量/ 获取整个成员变量列表Ivar * class_copyIvarList ( Class cls, u

6、nsigned int *outCount ); /必须使用free()来释放这个数组/属性操作函数/ 获取类中指定名称实例成员变量的信息Ivar class_getInstanceVariable ( Class cls, const char *name );/ 获取类成员变量的信息Ivar class_getClassVariable ( Class cls, const char *name );/ 添加成员变量BOOL class_addIvar ( Class cls, const char *name, size_t size, uint8_t alignment, const

7、char *types );/ 获取整个成员变量列表Ivar * class_copyIvarList ( Class cls, unsigned int *outCount );methodLists/ 添加方法BOOL class_addMethod ( Class cls, SEL name, IMP imp, const char *types ); /和成员变量不同的是可以为类动态添加方法。如果有同名会返回NO,修改的话需要使用method_setImplementation/ 获取实例方法Method class_getInstanceMethod ( Class cls, SEL

8、 name );/ 获取类方法Method class_getClassMethod ( Class cls, SEL name );/ 获取所有方法的数组Method * class_copyMethodList ( Class cls, unsigned int *outCount );/ 替代方法的实现IMP class_replaceMethod ( Class cls, SEL name, IMP imp, const char *types );/ 返回方法的具体实现IMP class_getMethodImplementation ( Class cls, SEL name );

9、IMP class_getMethodImplementation_stret ( Class cls, SEL name );/ 类实例是否响应指定的selectorBOOL class_respondsToSelector ( Class cls, SEL sel );objc_protocol_list/ 添加协议BOOL class_addProtocol ( Class cls, Protocol *protocol );/ 返回类是否实现指定的协议BOOL class_conformsToProtocol ( Class cls, Protocol *protocol );/ 返回

10、类实现的协议列表Protocol * class_copyProtocolList ( Class cls, unsigned int *outCount );version/ 获取版本号int class_getVersion ( Class cls );/ 设置版本号void class_setVersion ( Class cls, int version );实例/-/ MyClass.hinterface MyClass : NSObject property (nonatomic, strong) NSArray *array;property (nonatomic, copy)

11、NSString *string;- (void)method1;- (void)method2;+ (void)classMethod1;end/-/ MyClass.m#import MyClass.hinterface MyClass () NSInteger _instance1;NSString * _instance2;property (nonatomic, assign) NSUInteger integer;- (void)method3WithArg1:(NSInteger)arg1 arg2:(NSString *)arg2;endimplementation MyCla

12、ss+ (void)classMethod1 - (void)method1 NSLog(call method method1);- (void)method2 - (void)method3WithArg1:(NSInteger)arg1 arg2:(NSString *)arg2 NSLog(arg1 : %ld, arg2 : %, arg1, arg2);end/-/ main.h#import MyClass.h#import MySubClass.h#import int main(int argc, const char * argv) autoreleasepool MyCl

13、ass *myClass = MyClass alloc init; unsigned int outCount = 0; Class cls = myClass.class; / 类名 NSLog(class name: %s, class_getName(cls); NSLog(=); / 父类 NSLog(super class name: %s, class_getName(class_getSuperclass(cls); NSLog(=); / 是否是元类 NSLog(MyClass is % a meta-class, (class_isMetaClass(cls) ? : no

14、t); NSLog(=); Class meta_class = objc_getMetaClass(class_getName(cls); NSLog(%ss meta-class is %s, class_getName(cls), class_getName(meta_class); NSLog(=); / 变量实例大小 NSLog(instance size: %zu, class_getInstanceSize(cls); NSLog(=); / 成员变量 Ivar *ivars = class_copyIvarList(cls, &outCount); for (int i = 0

15、; i outCount; i+) Ivar ivar = ivarsi; NSLog(instance variables name: %s at index: %d, ivar_getName(ivar), i); free(ivars); Ivar string = class_getInstanceVariable(cls, _string); if (string != NULL) NSLog(instace variable %s, ivar_getName(string); NSLog(=); / 属性操作 objc_property_t * properties = class

16、_copyPropertyList(cls, &outCount); for (int i = 0; i outCount; i+) objc_property_t property = propertiesi; NSLog(propertys name: %s, property_getName(property); free(properties); objc_property_t array = class_getProperty(cls, array); if (array != NULL) NSLog(property %s, property_getName(array); NSL

17、og(=); / 方法操作 Method *methods = class_copyMethodList(cls, &outCount); for (int i = 0; i outCount; i+) Method method = methodsi; NSLog(methods signature: %s, method_getName(method); free(methods); Method method1 = class_getInstanceMethod(cls, selector(method1); if (method1 != NULL) NSLog(method %s, m

18、ethod_getName(method1); Method classMethod = class_getClassMethod(cls, selector(classMethod1); if (classMethod != NULL) NSLog(class method : %s, method_getName(classMethod); NSLog(MyClass is% responsd to selector: method3WithArg1:arg2:, class_respondsToSelector(cls, selector(method3WithArg1:arg2:) ?

19、 : not); IMP imp = class_getMethodImplementation(cls, selector(method1); imp(); NSLog(=); / 协议 Protocol * _unsafe_unretained * protocols = class_copyProtocolList(cls, &outCount); Protocol * protocol; for (int i = 0; i outCount; i+) protocol = protocolsi; NSLog(protocol name: %s, protocol_getName(pro

20、tocol); NSLog(MyClass is% responsed to protocol %s, class_conformsToProtocol(cls, protocol) ? : not, protocol_getName(protocol); NSLog(=); return 0;动态创建类和对象动态创建类/ 创建一个新类和元类Class objc_allocateClassPair ( Class superclass, const char *name, size_t extraBytes ); /如果创建的是root class,则superclass为Nil。extraB

21、ytes通常为0/ 销毁一个类及其相关联的类void objc_disposeClassPair ( Class cls ); /在运行中还存在或存在子类实例,就不能够调用这个。/ 在应用中注册由objc_allocateClassPair创建的类void objc_registerClassPair ( Class cls ); /创建了新类后,然后使用class_addMethod,class_addIvar函数为新类添加方法,实例变量和属性后再调用这个来注册类,再之后就能够用了。使用实例Class cls = objc_allocateClassPair(MyClass.class, M

22、ySubClass, 0);class_addMethod(cls, selector(submethod1), (IMP)imp_submethod1, v:);class_replaceMethod(cls, selector(method1), (IMP)imp_submethod1, v:);class_addIvar(cls, _ivar1, sizeof(NSString *), log(sizeof(NSString *), i);objc_property_attribute_t type = T, NSString;objc_property_attribute_t owne

23、rship = C, ;objc_property_attribute_t backingivar = V, _ivar1;objc_property_attribute_t attrs = type, ownership, backingivar;class_addProperty(cls, property2, attrs, 3);objc_registerClassPair(cls);id instance = cls alloc init;instance performSelector:selector(submethod1);instance performSelector:sel

24、ector(method1);动态创建对象/ 创建类实例id class_createInstance ( Class cls, size_t extraBytes ); /会在heap里给类分配内存。这个方法和+alloc方法类似。/ 在指定位置创建类实例id objc_constructInstance ( Class cls, void *bytes );/ 销毁类实例void * objc_destructInstance ( id obj ); /不会释放移除任何相关引用测试下效果/可以看出class_createInstance和alloc的不同id theObject = cla

25、ss_createInstance(NSString.class, sizeof(unsigned);id str1 = theObject init;NSLog(%, str1 class);id str2 = NSString alloc initWithString:test;NSLog(%, str2 class);实例操作函数这些函数是针对创建的实例对象的一系列操作函数。整个对象操作的函数/ 返回指定对象的一份拷贝id object_copy ( id obj, size_t size );/ 释放指定对象占用的内存id object_dispose ( id obj );应用场景/

26、把a转换成占用更多空间的子类bNSObject *a = NSObject alloc init;id newB = object_copy(a, class_getInstanceSize(MyClass.class);object_setClass(newB, MyClass.class);object_dispose(a);对象实例变量进行操作的函数/ 修改类实例的实例变量的值Ivar object_setInstanceVariable ( id obj, const char *name, void *value );/ 获取对象实例变量的值Ivar object_getInstan

27、ceVariable ( id obj, const char *name, void *outValue );/ 返回指向给定对象分配的任何额外字节的指针void * object_getIndexedIvars ( id obj );/ 返回对象中实例变量的值id object_getIvar ( id obj, Ivar ivar );/ 设置对象中实例变量的值void object_setIvar ( id obj, Ivar ivar, id value );对对象类操作/ 返回给定对象的类名const char * object_getClassName ( id obj );/ 返回对象的类Class object_getClass ( id obj );/ 设置对象的类Class object_setClass ( id obj, Class cls );获取类定义/ 获取已注册的类定义的列表int objc_getClassList ( Class *buffer, int bufferCount );/ 创建并返回一个指向所有已注册类的指针列表Class * objc_copyClassList ( unsigned int

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2