iOS動態性:動態添加屬性的方法——關聯(e.g. 向Category添加屬性)

來源:互聯網
上載者:User

標籤:type   lsp   exp   set   category   x64   nbsp   並且   alt   

想到要如何為所有的對象增加執行個體變數嗎?我們知道,使用Category可以很方便地為現有的類增加方法,但卻無法直接增加執行個體變數。不過從Mac OS X v10.6開始,系統提供了Associative References,這個問題就很容易解決了。這種方法也就是所謂的關聯(association),我們可以在runtime期間動態地添加任意多的屬性,並且隨時讀取。所用到的兩個重要runtime API是:

1 OBJC_EXPORT void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
12 OBJC_EXPORT id objc_getAssociatedObject(id object, const void *key)__OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);

現在我們結合一個實際的例子來說明他們的用法。假設我們現在打算利用category對UILabel進行屬性補充,添加FlashColor屬性。一般我們有個原則:能用category擴充就不用繼承,因為隨著繼承深度的增加,代碼的可維護性也會增加很多。使用category可以這麼做:

12345678 #import <UIKit/UIKit.h>#import <objc/runtime.h> @interface UILabel (Associate)//單單從標頭檔看是不是很像一個類?再看看.m檔案你就知道這些都是假象了 - (nonatomic, retain) UIColor *FlashColor; @end
12345678910111213 #import "UILabel+Associate.h" @implementation UILabel (Associate)<br><br>@dynamic FlashColor; static char flashColorKey; - (void) setFlashColor:(UIColor *) flashColor{    objc_setAssociatedObject(self, &flashColorKey, flashColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);}- (UIColor *) getFlashColor{   return objc_getAssociatedObject(self, &flashColorKey);}@end

上面的例子有幾個需要注意的地方:

1、key:我們注意到在函數簽名中key的類型const void *,這表示key僅僅是一個地址,而不是字串的內容,這也是為說明flashColorKey沒有初始化的原因,因為具體指向什麼內容我們無所謂,我們要的僅僅是地址!如果在setAssocaitedObject中你傳入的是flashColorKey,那get方法得到的值將會是nil。正確的應該是傳入地址&flashColorKey。

2、policy:這裡的policy跟屬性聲明中的retain、assign、copy是一樣的,不再贅述

3、在implement開始處的@dynamic聲明。一般來說@dynamic與@synthesize都可以用來聲明屬性,@synthesize是預設的聲明,意思是編譯器在編譯階段自動為你的屬性產生setter與getter;而@dynamic則告訴編譯器,別慌,小子,編譯階段不用為我產生setter與getter,在runtime我已經自己實現了setter與getter。此處我們選擇@dynamic。事實上,二者曾引起stackOverFlow上強烈的爭論:請點這裡。

下面我們再來看另一個例子,來源於APPLE GUIDE:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 #import <Foundation/Foundation.h>#import <objc/runtime.h>  int main (int argc, const char * argv[]) {      @autoreleasepool {    /*Seciton 0. 關聯資料的Key和Value*/    static char overviewKey;    static const char *myOwnKey = "VideoProperty\0";    static const char intValueKey = ‘i‘;      NSArray *array = [[NSArray alloc]            initWithObjects:@ "One", @"Two", @"Three"nil];     // For the purposes of illustration, use initWithFormat: to ensure    // we get a deallocatable string    NSString *overview = [[NSString alloc]            initWithFormat:@"%@", @"First three numbers"];    NSString *videoKeyValue = @"This is a video";    NSNumber *intValue = [[NSNumber alloc]initWithInt:5];     /*Section 1. 關聯資料設定部分*/    objc_setAssociatedObject (            array,            &overviewKey,            overview,            OBJC_ASSOCIATION_RETAIN        );        [overview release];     objc_setAssociatedObject (        array,        myOwnKey,        videoKeyValue,        OBJC_ASSOCIATION_RETAIN    );     objc_setAssociatedObject (        array,        &intValueKey,        intValue,        OBJC_ASSOCIATION_RETAIN    );      /*Section 3. 關聯資料查詢部分*/    NSString *associatedObject =  (NSString *) objc_getAssociatedObject (array, &overviewKey);    NSLog(@"associatedObject: %@", associatedObject);    NSString *associatedObject2 = (NSString *) objc_getAssociatedObject(array, myOwnKey);    NSLog(@"Video Key value is %@", associatedObject2);    NSString *assObject3 = (NSString *) objc_getAssociatedObject(array, &myOwnKey);    if( assObject3 )    {        NSLog(@"不會進入這裡! assObject3 應當為nil!");    }    else    {        NSLog(@"OK. 通過myOwnKey的地址是得不到資料的!");    }    NSNumber *assKeyValue = (NSNumber *) objc_getAssociatedObject(array, &intValueKey);    NSLog(@"Int value is %d",[assKeyValue intValue]);         /*Section 3. 關聯資料清理部分*/    objc_setAssociatedObject (            array,            &overviewKey,            nil,            OBJC_ASSOCIATION_ASSIGN        );     objc_setAssociatedObject (        array,        myOwnKey,        nil,        OBJC_ASSOCIATION_ASSIGN    );         objc_setAssociatedObject (        array,        &intValueKey,        nil,        OBJC_ASSOCIATION_ASSIGN    );        [array release];      }    return 0;}

編程小翁@部落格園,謝謝 前輩資料.

iOS動態性:動態添加屬性的方法——關聯(e.g. 向Category添加屬性)

聯繫我們

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