代码的优雅

创建线程

static dispatch_queue_t user_handel_queue(){
    static dispatch_queue_t user_handel_queue_t ;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        user_handel_queue_t = dispatch_queue_create("user_handel_queue_t", DISPATCH_QUEUE_CONCURRENT);
    });
    return user_handel_queue_t;
}

判断代理或者协议

下面的方式,一个标志为只会占用一个二进制,并且不用每次使用代理都去判断是否实现协议那样冗长的代码。很棒吧

struct {
    unsigned int sendSDPFlag        :1;
    unsigned int sendCandidateFlag  :1;
}_delegateFlag;

- (void)setDelegate:(id)delegate{
    _delegate = delegate;
    _delegateFlag.sendSDPFlag = [delegate respondsToSelector:@selector(needSendSDPToPeer:offer:)];
    _delegateFlag.sendCandidateFlag = [delegate respondsToSelector:@selector(needSendCandidateToPeer:)];
}
 if(_delegateFlag.sendCandidateFlag){
        [self.delegate needSendCandidateToPeer:candidateStr];
    }

你可能感兴趣的:(代码的优雅)