iOS va_list,va_start,va_end

來源:互聯網
上載者:User

標籤:


- (id)initWithTitle:(NSString *)title
            message:(NSString *)message
    completionBlock:(void (^)(NSUInteger buttonIndex, EMAlertView *alertView))block
  cancelButtonTitle:(NSString *)cancelButtonTitle
  otherButtonTitles:(NSString *)otherButtonTitles, ... {
    //#import <objc/runtime.h>標頭檔
     //objc_setAssociatedObject需要四個參數:來源物件,關鍵字,關聯的對象和一個關聯策略。
    objc_setAssociatedObject(self, "blockCallback", [block copy], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    if (self = [self initWithTitle:title message:message delegate:self cancelButtonTitle:nil otherButtonTitles:nil])
    {
        if (cancelButtonTitle) {
            [self addButtonWithTitle:cancelButtonTitle];
            self.cancelButtonIndex = [self numberOfButtons] - 1;
        }
        id eachObject;
        va_list argumentList;//va_list 是一個字元指標,可以理解為指向當前參數的一個指標,取參必須通過這個指標進行
        if (otherButtonTitles) {
            [self addButtonWithTitle:otherButtonTitles];
            va_start(argumentList, otherButtonTitles);// 然後應該對argumentList進行初始化,讓它指向可變參數表裡面的第一個參數,這是通過 va_start 來實現的,第一個參數是argumentList本身,第二個參數是在變參表前面緊挨著的一個變數,即“...”之前的那個參數
            while ((eachObject = va_arg(argumentList, id))) {//然後是擷取參數,調用va_arg,它的第一個參數是argumentList,第二個參數是要擷取的參數的指定類型,然後返回這個指定類型的值,並且把argumentList的位置指向變參表的下一個變數位置
                [self addButtonWithTitle:eachObject];
            }
            va_end(argumentList);//置空argumentList//擷取所有的參數之後,我們有必要將這個argumentList指標關掉,以免發生危險,方法是調用 va_end,它使輸入的參數argumentList置為 NULL,應該養成擷取完參數表之後關閉指標的習慣。說白了,就是讓我們的程式具有健壯性。通常va_start和va_end是成對出現。
        }
    }
    return self;
}


iOS va_list,va_start,va_end

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.