美文网首页
【知识总结】写法

【知识总结】写法

作者: 小子爱搞事 | 来源:发表于2017-02-24 16:34 被阅读75次

    1,默认的初始化数据,在初始化方法中进行设置

    比如:NSInterge _selectedIndex;
    默认情况下设置为 _selectedIndex = 1;
    如果在 viewDidload 方法中进行默认是的设置 ,外界在设置 _selectedIndex = 2 的时机在 viewDidload 之前,则这次的设置就会被重置为 1。

    因此,初始化的默认值推荐在初始化中进行设置


    XMNetworking

    2,队列写法

    static dispatch_queue_t xm_request_completion_callback_queue() {
        static dispatch_queue_t _xm_request_completion_callback_queue;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _xm_request_completion_callback_queue = dispatch_queue_create("com.xmnetworking.request.completion.callback.queue", DISPATCH_QUEUE_CONCURRENT);
        });
        return _xm_request_completion_callback_queue;
    }
    

    上面的这种书写方式,便于之后的队列调用,如:

    dispatch_async(xm_request_completion_callback_queue(), ^{
                    completionHandler(nil, serializationError);
                });
    

    3,信号量:使用宏定义

    dispatch_semaphore_t _lock;
    _lock = dispatch_semaphore_create(1);
    
    #define XMLock() dispatch_semaphore_wait(self->_lock, DISPATCH_TIME_FOREVER)
    #define XMUnlock() dispatch_semaphore_signal(self->_lock)
    

    4,block 使用 宏 调用

    #define XM_SAFE_BLOCK(BlockName, ...) ({ !BlockName ? nil : BlockName(__VA_ARGS__); })
    
    // 调用:
    XM_SAFE_BLOCK(self.requestProcessHandler, request);
    

    AOP 中的方法替换写法:

    + (void)load{
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            Class class = [self class];
            
            SEL originalSelector = @selector(setCachePolicy:);
            SEL swizzledSelector = @selector(ag_setCachePolicy:);
            
            Method originalMethod = class_getInstanceMethod(class, originalSelector);
            Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
            
            BOOL success = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
            if (success) {
                class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
            } else {
                method_exchangeImplementations(originalMethod, swizzledMethod);
            }
        });
    }
    

    多个方法需要替换,可以使用下面方式

    + (void)load {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            NSArray *selStringsArray = @[@"reloadData", @"reloadRowsAtIndexPaths:withRowAnimation:", @"deleteRowsAtIndexPaths:withRowAnimation:", @"insertRowsAtIndexPaths:withRowAnimation:"];
            [selStringsArray enumerateObjectsUsingBlock:^(NSString *selString, NSUInteger idx, BOOL *stop) {
                NSString *mySelString = [@"sd_" stringByAppendingString:selString];
                
                Method originalMethod = class_getInstanceMethod(self, NSSelectorFromString(selString));
                Method myMethod = class_getInstanceMethod(self, NSSelectorFromString(mySelString));
                method_exchangeImplementations(originalMethod, myMethod);
            }];
        });
    }
    

    相关文章

      网友评论

          本文标题:【知识总结】写法

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