(转发)IOS高级开发~Runtime(三)

时间:2022-11-02 10:11:34

11、系统类的方法实现部分替换

- (void) methodExchange {

Method m1 = class_getInstanceMethod([NSStringclass],@selector(lowercaseString));

Method m2 = class_getInstanceMethod([NSStringclass],@selector(uppercaseString));

method_exchangeImplementations(m1, m2);

NSLog(@"%@", [@"sssAAAAss"lowercaseString]);

NSLog(@"%@", [@"sssAAAAss"uppercaseString]);

}

打印结果:(仔细看log)

2013-07-26 16:33:22.776 HighOC[7104:c07] SSSAAAASS

2013-07-26 16:33:22.778 HighOC[7104:c07] sssaaaass

12、自定义类的方法实现部分替换

- (void) justLog1 {

NSLog(@"justLog1");

}

- (void) justLog2 {

NSLog(@"justLog2");

}

- (void) methodSetImplementation {

Method method =
class_getInstanceMethod([ClassMethodViewCtrclass],@selector(justLog1));

IMP originalImp = method_getImplementation(method);

Method m1 = class_getInstanceMethod([ClassMethodViewCtrclass],@selector(justLog2));

method_setImplementation(m1, originalImp);

}

//[self methodSetImplementation];

//[self justLog2];

13、覆盖系统方法

IMP cFuncPointer;

IMP cFuncPointer1;

IMP cFuncPointer2;

NSString* CustomUppercaseString(idself,SEL_cmd){

printf("真正起作用的是本函数CustomUppercaseString\r\n");

NSString *string = cFuncPointer(self,_cmd);

return string;

}

NSArray* CustomComponentsSeparatedByString(idself,SEL_cmd,NSString *str){

printf("真正起作用的是本函数CustomIsEqualToString\r\n");

return cFuncPointer1(self,_cmd, str);

}

//不起作用,求解释

bool CustomIsEqualToString(idself,SEL_cmd,NSString *str) {

printf("真正起作用的是本函数CustomIsEqualToString\r\n");

return cFuncPointer2(self,_cmd, str);

}

- (void) replaceMethod{

cFuncPointer = [NSStringinstanceMethodForSelector:@selector(uppercaseString)];

class_replaceMethod([NSStringclass],@selector(uppercaseString), (IMP)CustomUppercaseString,"@@:");

cFuncPointer1 = [NSStringinstanceMethodForSelector:@selector(componentsSeparatedByString:)];

class_replaceMethod([NSStringclass],@selector(componentsSeparatedByString:), (IMP)CustomComponentsSeparatedByString,"@@:@");

cFuncPointer2 = [NSStringinstanceMethodForSelector:@selector(isEqualToString:)];

class_replaceMethod([NSStringclass],@selector(isEqualToString:), (IMP)CustomIsEqualToString,"B@:@");

}

14、自动序列化(转)

#import "NSObject+AutoEncodeDecode.h"

@implementation NSObject (AutoEncodeDecode)

- (void)encodeWithCoder:(NSCoder *)encoder {

Class cls = [selfclass];

while (cls != [NSObjectclass]) {

unsigned int numberOfIvars =0;

Ivar* ivars = class_copyIvarList(cls, &numberOfIvars);

for(const
Ivar* p = ivars; p < ivars+numberOfIvars; p++){

Ivar const ivar = *p;

const char *type =ivar_getTypeEncoding(ivar);

NSString *key = [NSStringstringWithUTF8String:ivar_getName(ivar)];

id value = [selfvalueForKey:key];

if (value) {

switch (type[0]) {

case _C_STRUCT_B: {

NSUInteger ivarSize =0;

NSUInteger ivarAlignment =0;

NSGetSizeAndAlignment(type, &ivarSize, &ivarAlignment);

NSData *data = [NSDatadataWithBytes:(constchar *)self +
ivar_getOffset(ivar)

length:ivarSize];

[encoder encodeObject:dataforKey:key];

}

break;

default:

[encoder encodeObject:value

forKey:key];

break;

}

}

}

free(ivars);

cls = class_getSuperclass(cls);

}

}

- (id)initWithCoder:(NSCoder *)decoder {

self = [self
init];

if (self) {

Class cls = [selfclass];

while (cls != [NSObjectclass]) {

unsigned int numberOfIvars =0;

Ivar* ivars =
class_copyIvarList(cls, &numberOfIvars);

for(constIvar* p = ivars; p < ivars+numberOfIvars; p++){

Ivar const ivar = *p;

const char *type =ivar_getTypeEncoding(ivar);

NSString *key = [NSStringstringWithUTF8String:ivar_getName(ivar)];

id value = [decoder
decodeObjectForKey:key];

if (value) {

switch (type[0]) {

case
_C_STRUCT_B: {

NSUInteger ivarSize =0;

NSUInteger ivarAlignment =0;

NSGetSizeAndAlignment(type, &ivarSize, &ivarAlignment);

NSData *data = [decoderdecodeObjectForKey:key];

char *sourceIvarLocation = (char*)self+ivar_getOffset(ivar);

[data getBytes:sourceIvarLocationlength:ivarSize];

}

break;

default:

[self
setValue:[decoder decodeObjectForKey:key]

forKey:key];

break;

}

}

}

free(ivars);

cls = class_getSuperclass(cls);

}

}

return self;

}

IOS高级开发~Runtime(一)

http://blog.csdn.net/zfpp25_/article/details/9496705

IOS高级开发~Runtime(二)

http://blog.csdn.net/zfpp25_/article/details/9497187

IOS高级开发~Runtime(三)

http://blog.csdn.net/zfpp25_/article/details/9497721

IOS高级开发~Runtime(四)

http://blog.csdn.net/zfpp25_/article/details/9498233

(转发)IOS高级开发~Runtime(三)的更多相关文章

  1. &lpar;转发&rpar;IOS高级开发~Runtime(四)

    用C代替OC: #import <objc/runtime.h> #import <objc/message.h> #import <stdio.h> extern ...

  2. &lpar;转发&rpar;IOS高级开发~Runtime(二)

    一些公用类: @interface ClassCustomClass :NSObject{ NSString *varTest1; NSString *varTest2; NSString *varT ...

  3. &lpar;转发&rpar;IOS高级开发~Runtime(一)

    IOS高级开发-Runtime(一) IOS高级开发-Runtime(二) IOS高级开发-Runtime(三) IOS高级开发-Runtime(四) 一些公用类: @interface Custom ...

  4. iOS 高级开发 runtime(三)

    三 .动态添加方法 我们可以通过runtime动态地添加方法.那么到底啥叫动态添加方法呢?动态添加方法就是当我们程序运行时才知道我们应该调用哪个方法.我们首先需要了解这一点,当我们编写完一段代码后,我 ...

  5. IOS高级开发 runtime(一)

    一. 简介 IOS 开发中灵活使用runtime 会提高我们的程序性能和开发速度.要想使用runtime,首先要引入系统的头文件. <span style="font-size:18p ...

  6. IOS 高级开发 runtime(二)

    二.移魂大法 使用runtime还可以交换两个函数.先贴上代码和执行结果. #import <Foundation/Foundation.h> @interface DZLPerson : ...

  7. IOS高级开发之多线程(四)NSOperation

    1.什么是NSOperation,NSOperationQueue? NSOperation是一个抽象的基类,表示一个独立的计算单元,可以为子类提供有用且线程安全的建立状态,优先级,依赖和取消等操作. ...

  8. ios高级开发之多线程(三)GCD技术

    GCD是基于C的API,它是libdispatch的的市场名称.而libdispatch作为Apple公司的一个库,为并发代码在多核硬件(跑IOS或者OS X)上执行提供有力支持. 那么我们为什么要用 ...

  9. IOS高级开发~Runtime(一)

    #import <Foundation/Foundation.h> @interface CustomClass : NSObject -(void)fun1; @end @interfa ...

随机推荐

  1. event事件对象

    事件对象event: 在触发DOM事件的时候都会产生一个对象 1.type:获取事件类型 2.target:获取事件目标 3.stopPropagation():组织事件冒泡 4.preventDef ...

  2. github代码收集推荐

    https://github.com/AFNetworking/AFNetworkinghttps://github.com/jessesquires/JSQMessagesViewControlle ...

  3. each循环

    var NA_COUNT=0; var NG_OK_COUNT=0; //获取所有检验明细为同一个编号的下拉选项,看有没有不是N/A的下拉选项 $("#@(Perfix)tbData sel ...

  4. MTD技术介绍

    MTD(Memory Technology device)是用于访问memory设备(ROM.Flash)的Linux子系统,在Linux中引入这一层的主要目的是为了更加简单的添加新的Memory存储 ...

  5. CSS的浮动和清除

    CSS浮动和清除 什么是浮动? 在实现效果上,让元素浮起来(飘起来),动起来(向左或向右移动) 浮动本质:就是将两个块元素同存一行. float 取值:主要是对浮动的方向进行控制 left:让元素向左 ...

  6. Xcode6 UIWebView与JavaScript交互(issue fix)

    这篇文章中,有介绍UIWebView与JavaScript交互,在UIWebView截获JavaScript请求处理.从app的角度,这是JavaScript的Hook请求. 在Xcode6之前的Ap ...

  7. 【算法】赫夫曼树(Huffman)的构建和应用(编码、译码)

    参考资料 <算法(java)>                           — — Robert Sedgewick, Kevin Wayne <数据结构>       ...

  8. CentOS升级Svn到最新版

    CentOS升级Svn到最新版(金庆的专栏)CentOS/RHEL yum 安装的 subversion 是 1.6.11 版本,连VisulaSVN服务器时会有"Key usage vio ...

  9. 关于form-checkbox 必填项无效的错误

    校验规则要写在一个form里 检查data,给个默认值. 否则刚进去错误提示不显示. 其次,要加 type 类型,注意大小写 触发类型为 change .

  10. 【FairyGUI &amp&semi; Unity】实现血条UI扣血与加血的缓动效果

    组件设计 创建一个进度条组件,作为血条. bar是实际血量条 DownBar是扣血缓动背景图层 UpBar是加血缓动背景图层 LowBar是低血量变色(和控制器配合,本文不讲) n11组合是血量参考线 ...