iOS 调试技巧(一)

很多人给工程调试时,每个界面都写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;

    }else if (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];

    unsigned int count = 0;

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

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

        objc_property_t property = properties[i];

        const char *cProperty = property_getName(property);

        id ocProperty = [NSString stringWithUTF8String:cProperty];

        id value = [self valueForKey:ocProperty];

    

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

    }

   

    return descri;

}

#endif

@end

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值