美文网首页
iOS - OC <<编写高质量iOS与OS X代码的多个有效方

iOS - OC <<编写高质量iOS与OS X代码的多个有效方

作者: 洧中苇_4187 | 来源:发表于2020-09-22 10:02 被阅读0次

    19.用"僵尸对象"调试内存管理问题

    #import <UIKit/UIKit.h>
    #import "AppDelegate.h"
    #import <objc/runtime.h>
    
    @interface EOCClass : NSObject
    @end
    
    @implementation EOCClass
    @end
    
    void PrintClassInfo(id obj) {
        Class cls = object_getClass(obj);
        Class superCls = class_getSuperclass(cls);
        NSLog(@"===%s : %s===",class_getName(cls),class_getName(superCls));
    }
    int main(int argc, char * argv[]) {
        
        EOCClass *obj = [[EOCClass alloc]init];
        NSLog(@"Before release:");
        
        PrintClassInfo(obj);
        
        [obj release];
        
        NSLog(@"After release:");
    
        PrintClassInfo(obj);
        
        NSString *desc = [obj description];
    }
    
    //打印结果
    2020-09-21 15:33:45.965639+0800 testMRC[76903:4034848] Before release:
    2020-09-21 15:33:45.966274+0800 testMRC[76903:4034848] ===EOCClass : NSObject===
    2020-09-21 15:33:45.966385+0800 testMRC[76903:4034848] After release:
    2020-09-21 15:33:45.966492+0800 testMRC[76903:4034848] ===_NSZombie_EOCClass : nil===
    2020-09-21 15:33:45.966639+0800 testMRC[76903:4034848] *** -[EOCClass description]: message sent to deallocated instance 0x600001304090
    (lldb) 
    

    1.通过打印我们知道,开启捕获僵尸对象后,系统会将本来的类加一个_NSZombie_前缀,这个套路跟KVO一模一样;
    2.当开启僵尸对象之后,其类的isa指针指向了新创建的僵尸类,僵尸类能够响应所有方法,响应方式为:打印一条包含消息内容及接收者的消息,然后终止应用程序.

    20.为常用的Block类型创建typedef

    
    typedef int(EOCSomeBlock) (BOOL flag ,int value);
    @interface EOCClass : NSObject
    @property(nonatomic,copy)EOCSomeBlock *someBlock;
    @end
    
    @implementation EOCClass
    @end
    

    1.以typedef重新定义Block类型,可令Block用起来更简单.
    2.定义新类型时应遵循现有的命名习惯,勿使其名称与别的名称冲突.
    3.不妨为同一个Block简明定义多个类型别名,如果要用重构条的代码使用了Block类型的某个别名,那么值需修改相应的typedef中Block签名即可,无需改动其他typedef.

    21.多用派发队列,少用同步锁

    //同步方案一
    - (void)synchronizedMethod {
        @synchronized(self) {
            //Safe
        }
    }
    
    //同步方案二
    @interface ViewController ()
    @property(nonatomic,strong)NSLock *lock;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        _lock = [NSLock new];
    }
    
    - (void)synchronizedMethod
    {
        [_lock lock];
        //Safe
        [_lock unlock];
    }
    

    1.将同步与异步派发结合起来,可实现与普通枷锁机制一样的同步行为,不会租在执行异步派发线程.
    2.使用同步队列及栅栏块,可以令同步行为更加高效
    3.GCD 和 NSOperationQueue ,GCD就像二哈一样,松手就不用管了,你不能阻止他,不能取消,而NSOperationQueue就像边牧,天生有服从的血统,它能很好的理解你的意思并执行

    22.构建缓存时选用NSCache 而非NSDictionary

    1.NSCache低内存时的自动清除功能
    2.NSCache自动删减久未使用的文件
    3.NSCache不会拷贝键,而是会保留它
    4.NSCache是线程安全的
    5.NSCache可以设置上限,超出阈值,系统会根据情况合理删减

    22.别忘了NSTimer会保留其目标对象

    1.定时器一定要放到RunLoop里面
    2.销毁定时器一定要invalidate
    3.NSTimer容易造成循环引用,一定要记得不用的时候做清空操作

    相关文章

      网友评论

          本文标题:iOS - OC <<编写高质量iOS与OS X代码的多个有效方

          本文链接:https://www.haomeiwen.com/subject/azpvyktx.html