zoukankan      html  css  js  c++  java
  • (转发)IOS高级开发~Runtime(三)

    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 ");

        NSString *string = cFuncPointer(self,_cmd);

        return string;

    }

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

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

        return cFuncPointer1(self,_cmd, str);

    }

    //不起作用,求解释

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

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

        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

  • 相关阅读:
    web前端工程师
    java工程师
    原因原来默认预检测会检测是否存在多选框
    软件测试&安全测试高峰论坛
    安卓学习图
    为什么mongo中不能用int作为key
    历经小半宿吧。哎,终于搭建好了Linux-C的环境
    把昨晚写的东西完善了一下,还行,真差不多
    半宿了,仿写了个CList模板类,留着以后用吧
    今天复习了一下完成端口网络模型
  • 原文地址:https://www.cnblogs.com/MengXY/p/4156726.html
Copyright © 2011-2022 走看看