雜記

1.const 修飾的右邊的部分,不可改變編程

2.static 修飾的變量,程序結束時才釋放,變量的地址只建立一次數組

3.宏 指定的字符串,並不會建立屢次,而是隻建立一個,它增長的只是預編譯的時間xcode

4.extern工做原理:先會去當前文件下查找有沒有對應全局變量,若是沒有,纔會去其餘文件查找數據結構

5.https的非對稱加密,只是在握手時進行一次確認對稱加密的密鑰,以後大多數都用這個密鑰通訊,至於這個密鑰何時失效,還不清楚。佈局

6.抗壓縮優先級優化

兩個水平佈局的label,兩邊間隔分別是12,中間間隔爲8(懂意思就行)。若是兩個label 都不設置寬度,則左邊 label 會拉長,右邊 label 自適應。動畫

UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectZero];
    label1.backgroundColor = [UIColor redColor];
    label1.text = @"我是標題";
    [self.view addSubview:label1];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.view);
        make.left.equalTo(@(12));
    }];
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectZero];
    label2.backgroundColor = [UIColor redColor];
    label2.text = @"我是描述";
    [self.view addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(label1);
        make.left.equalTo(label1.mas_right).offset(8);
        make.right.equalTo(self.view).offset(-12);
    }];

若是想讓左邊 label 自適應,右邊 label 拉昇,能夠設置控件拉昇阻力(即抗拉昇),拉昇阻力越大越不容易被拉昇。因此只要 label1 的拉昇阻力比 label2 的大就能達到效果。ui

//UILayoutPriorityRequired = 1000 
    [label1 setContentHuggingPriority:UILayoutPriorityRequired
                              forAxis:UILayoutConstraintAxisHorizontal];
//    //UILayoutPriorityDefaultLow = 250
    [label2 setContentHuggingPriority:UILayoutPriorityDefaultLow
                              forAxis:UILayoutConstraintAxisHorizontal];

Content Hugging Priority:拉伸阻力,即抗拉伸。值越大,越不容易被拉伸。加密

Content Compression Resistance Priority:壓縮阻力,即抗壓縮。值越大,越不容易被壓縮。spa

7.約束優先級

從左到右依次爲紅、藍、黃三個視圖三等分,藍色視圖佈局依賴紅色,黃色視圖佈局依賴藍色,若是忽然將中間的藍色視圖移除,紅色和黃色視圖的寬度就沒法計算。此種狀況能夠設置最後一個黃色視圖的作約束優先級,移除中間藍色視圖後,紅色和黃色視圖二等分。

//紅  left bottom height
    [redView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).with.offset(20);
        make.bottom.mas_equalTo(self.view.mas_bottom).with.offset(-80);
        make.height.equalTo(@50);
    }];
    //藍   left bottom height       width=紅色
    [blueView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(redView.mas_right).with.offset(40);
        make.height.width.bottom.mas_equalTo(redView);
    }];
    //黃 left right height           width=紅色
    [yellowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(blueView.mas_right).with.offset(40);
        make.right.mas_equalTo(self.view.mas_right).with.offset(-20);
        make.height.width.bottom.mas_equalTo(redView);
        //優先級
        //必須添加這個優先級,不然blueView被移除後,redView 和 yellowView 的寬度就不能計算出來
        make.left.mas_equalTo(redView.mas_right).with.offset(20).priority(250);
    }];
    //移除藍色
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [blueView removeFromSuperview];
        [UIView animateWithDuration:3 animations:^{
            //不加這行代碼就直接跳到對應的地方,加這行代碼就能夠執行動畫。
            //另外還要注意調用layoutIfNeeded的對象必須是執行動畫的父視圖。
            //[blueView.superview layoutIfNeeded];
            [self.view layoutIfNeeded];
        }];
    });

8.爲何數組下標從零開始

數組下標最確切的定義應該偏移(offset),若是用 a 來表示數組的首地址,a[0]  就是偏移爲 0 的位置,也就是首地址,a[k] 就表示偏移 k 個 type_size 的位置,因此計算 a[k] 的內存地址只須要用這個公式:

a[k]_address = base_address + k * type_size

可是,若是數組從 1 開始計數,那咱們計算數組元素 a[k]的內存地址就會變爲:

a[k]_address = base_address + (k-1)*type_size

對比兩個公式,不難發現,從 1 開始編號,每次隨機訪問數組元素都多了一次減法運算,對於 CPU 來講,就是多了一次減法指令。數組做爲很是基礎的數據結構,經過下標隨機訪問數組元素又是其很是基礎的編程操做,效率的優化就要儘量作到極致。因此爲了減小一次減法操做,數組選擇了從 0 開始編號,而不是從 1 開始。

9.經過zone實現單例

+ (instancetype)sharedInstance {
    return [[self alloc] init];
}
- (instancetype)init {
    if (self = [super init]) {
         
    }
    return self;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    static LogManager * _sharedInstanc = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstanc = [super allocWithZone:zone];//最早執行,只執行了一次
    });
    return _sharedInstanc;
}
1
2
3
-(id)copyWithZone:(struct _NSZone *)zone{
    return [LogManager sharedInstance];
}
1
2
3
-(id)mutableCopyWithZone:(NSZone *)zone{
    return [LogManager sharedInstance];
}

8.weakSelf和strongSelf

weakSelf 是爲了block不持有self,避免Retain Circle循環引用。在 Block 內若是須要訪問 self 的方法、變量,建議使用 weakSelf。 strongSelf的目的是由於一旦進入block執行,假設不容許self在這個執行過程當中釋放,就須要加入strongSelf。block執行完後這個strongSelf 會自動釋放,沒有不會存在循環引用問題。若是在 Block 內須要屢次 訪問 self,則須要使用 strongSelf。

9.xcode打包路徑/Users/yanxuefeng/Library/Developer/Xcode/Archives

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息