iOS工程调试小技巧一:交换dealloc、字体方法,重写description

来源:互联网 发布:php类与对象person 编辑:程序博客网 时间:2024/05/29 23:46

很多人给工程调试时,每个界面都写dealloc,能达到效果,未免有些麻烦。下面介绍一个小小实例:

控制器写个分类,用runtime方法交换。

一、控制器、View分类

1.新建分类

#import "UIViewController+XYController.h"


2.m引入头文件 

#import <objc/runtime.h>



3.实现如下两个方法

+ (void)load{

    

    Method m1 = class_getInstanceMethod([self class], NSSelectorFromString(@

                                        "dealloc"));

    Method m2 = class_getInstanceMethod([self class], @selector(xy_dealloc));

    

    method_exchangeImplementations(m1, m2);

}


- (void)xy_dealloc

{

    NSLog(@"xy_dealloc - %@",[self class]);

    [self xy_dealloc];

}



二、字体适配也可交换方法

+ (void)load

{

    

    Method m1 = class_getClassMethod(self,@selector(systemFontOfSize:));

    Method m2 = class_getClassMethod(self,@selector(MDJ_ystemFontOfSize:));


    method_exchangeImplementations(m1, m2);

    

}


+ (void)MDJ_ystemFontOfSize:(CGFloat)fontSize

{

    MDJLog(@"----");

    // iPhone6屏幕对角线为基准,计算不同机型的比例,根据项目可以适当调整比例。

    if (Iphone5){

        fontSize -=1;

    }elseif (Iphone6Plus){

        fontSize +=1;

    }

    [self MDJ_ystemFontOfSize:fontSize];

}


三、给NSObject写分类,调试打印整齐,易于查看

#import "NSObject+MDJExtnestion.h"

#import <objc/message.h>


@implementation NSObject (MDJExtnestion)


#define MYTest2

#ifdef MYTest

- (NSString *)description

{

    NSMutableString *descri = [NSMutableString string];

    unsignedint count = 0;

    objc_property_t *properties = class_copyPropertyList([self class], &count);

    for (int i =0; i < count; i ++) {

        objc_property_t property = properties[i];

        constchar *cProperty = property_getName(property);

        id ocProperty = [NSString stringWithUTF8String:cProperty];

        id value = [self valueForKey:ocProperty];

    

        [descri appendFormat:@"  %@ : %@  ",ocProperty,value];

    }

   

    return descri;

}

#endif

@end




1 0
原创粉丝点击