iOS 開發中的單例,iOS開發

來源:互聯網
上載者:User

iOS 開發中的單例,iOS開發

  

  在iOS開發中經常會用到單例,比如每個iOS程式本身就是一個單例,在比如進行個人喜好設定儲存的時候用的也是一個單例。那我們如何自己來寫一個單例類呢,用自己的單例對象呢?下面是我寫的一個單例的標頭檔裡的代碼,這個檔案主要是一些宏。使用步驟寫的都很詳細,ARC或MRC都可以使用。有了這個標頭檔,只要在使用的時候包含這個檔案,基本上就OK了。具體怎麼使用,下面有詳細說明。

 

//  Singleton.h

//  單例的宏

/*

 使用方法

        1:包含這個標頭檔

        2:在.h檔案總包含 singleton_h(name) 裡面的name是你想要產生單例對象時的名字

        3:在.m檔案中包含 singleton_m(name) 裡面的name是你想要產生單例對象時的名字

           注意2、3步中的name要一致

        4:寫init初始化方法,因為每個單例類初始化的內容不一樣,所以沒有寫到宏裡面,在建立單例類時記得寫init方法

        5: 建立單例對象 [ 類名 share+(name)]

例如我們建立一個Person類的單例: 1 在Person的.h 檔案中 singleton_h(Person)

                             2 在Person的.m 檔案中 singleton_m(Person)

                             3 在Person的.m檔案中寫初始化方法

                             -(instancetype)init{

                                static id obj;

                                static dispatch_once_t onceToken;

                                dispatch_once(&onceToken, ^{

                                if ((obj = [super init])) {

                                //這裡進行本類的初始化

                                    }

                                });

                                self = obj;

                                return self;

                             }

                             }

                             4 在使用的地方包含Person這個類的標頭檔 [ Person sharePerson ];

// ## :連接字串和參數

// \ :表示下一行也是當前行的內容

 */

#ifndef Singleton_h

#define Singleton_h

 

#define singleton_h(name) +(instancetype)share##name;

#if __has_feature(objc_arc) // ARC

 

#define singleton_m(name) \

static id instance;\

+(instancetype)allocWithZone:(struct _NSZone *)zone{\

\

static dispatch_once_t onceToken;\

dispatch_once(&onceToken, ^{\

instance = [super allocWithZone:zone];\

});\

\

return instance;\

}\

\

+(instancetype)share##name{\

return [[self alloc]init];\

}\

+(id)copyWithZone:(struct _NSZone *)zone{\

return instance;\

}

 

#else //非ARC

 

#define singleton_m(name) \

static id instance;\

+(instancetype)allocWithZone:(struct _NSZone *)zone{\

    \

    static dispatch_once_t onceToken;\

    dispatch_once(&onceToken, ^{\

        instance = [super allocWithZone:zone];\

    });\

    \

    return instance;\

}\

\

+(instancetype)share##name{\

    return [[self alloc]init];\

}\

\

-(oneway void)release{\

    \

    \

}\

-(instancetype)autorelease{\

    return instance;\

}\

\

-(instancetype)retain{\

    return instance;\

}\

\

+(id)copyWithZone:(struct _NSZone *)zone{\

    return instance;\

}\

\

-(NSUInteger)retainCount{\

    return 1;\

}

 

#endif

#endif

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.