objc_runtime給類目添加屬性關聯(objc_setAssociatedObject、objc_getAssociatedObject)

       我們剛學習OC的時候知道類目是不可以添加屬性的,其實這不是絕對的,我們可以通過objc_setAssociatedObject動態添加屬性,和類進行關聯,那麼首先了解下什麼是關聯:


    關聯是指把兩個對象相互關聯起來,使得其中的一個對象作爲另外一個對象的一部分。
    關聯特性只有在Mac OS X V10.6以及以後的版本上纔是可用的。

在類的定義之外爲類增加額外的存儲空間

    使用關聯,我們可以不用修改類的定義而爲其對象增加存儲空間。這在我們無法訪問到類的源碼的時候或者是考慮到二進制兼容性的時候是非常有用。
    關聯是基於關鍵字的,因此,我們可以爲任何對象增加任意多的關聯,每個都使用不同的關鍵字即可。關聯是可以保證被關聯的對象在關聯對象的整個生命週期都是可用的(在垃圾自動回收環境下也不會導致資源不可回收)。

創建關聯

    創建關聯要使用到Objective-C的運行時函數:objc_setAssociatedObject來把一個對象與另外一個對象進行關聯。該函數需要四個參數:源對象,關鍵字,關聯的對象和一個關聯策略。當然,此處的關鍵字和關聯策略是需要進一步討論的。
  ■  關鍵字是一個void類型的指針。每一個關聯的關鍵字必須是唯一的。通常都是會採用靜態變量來作爲關鍵字。
  ■  關聯策略表明了相關的對象是通過賦值,保留引用還是複製的方式進行關聯的;還有這種關聯是原子的還是非原子的。這裏的關聯策略和聲明屬性時的很類似。這種關聯策略是通過使用預先定義好的常量來表示的。
    下面的代碼展示瞭如何把一個字符串關聯到一個數組上。
列表7-1 把一個字符串關聯到一個數組
  1. static char overviewKey;  
  2. NSArray * array =[[NSArray alloc] initWidthObjects:@"One", @"Two", @"Three", nil];  
  3. //爲了演示的目的,這裏使用initWithFormat:來確保字符串可以被銷燬  
  4. NSString * overview = [[NSString alloc] initWithFormat:@"@",@"First three numbers"];  
  5. objc_setAssociatedObject(array, &overviewKey, overview, OBJC_ASSOCIATION_RETAIN);  
  6.   
  7. [overview release];  
  8. //(1) overview仍然是可用的  
  9.   
  10. [array release];  
  11. //(2)overview 不可用  
    在(1)處,字符串overview仍然是可用的,這是因爲OBJC_ASSOCIATION_RETAIN策略指明瞭數組要保有相關的對象。當數組array被銷燬的時候,也就是在(2)處overview也就會被釋放,因此而被銷燬。如果此時還想使用overview,例如想通過log來輸出overview的值,則會出現運行時異常。

獲取相關聯的對象

    獲取相關聯的對象時使用Objective-C函數objc_getAssociatedObject。接着上面列表7-1的代碼,我們可以使用如下代碼來獲取與array相關聯的字符串:
  1. NSString * associatedObject = (NSString *)objc_getAssociatedObject(array, &oveviewKey);  

斷開關聯

    斷開關聯是使用objc_setAssociatedObject函數,傳入nil值即可。
    接着列表7-1中的程序,我們可以使用如下的代碼來斷開字符串overview和arry之間的關聯:
  1. objc_setAssociatedObject(array, &overviewKey, nil, OBJC_ASSOCIATION_ASSIGN);  
   其中,被關聯的對象爲nil,此時關聯策略也就無關緊要了。
    使用函數objc_removeAssociatedObjects可以斷開所有關聯。通常情況下不建議使用這個函數,因爲他會斷開所有關聯。只有在需要把對象恢復到“原始狀態”的時候纔會使用這個函數。

一個完整的實例程序

    下面的程序綜合了前面的代碼.
  1. #import <Foundation/Foundation.h>  
  2. #import <objc/runtime.h>  
  3.   
  4.   
  5. int main(int argc, const char* argv[])  
  6. {  
  7.     NSAutoreleasePool * pool = [[NSAutoreleasePool] alloc init];  
  8.       
  9.     static char overviewKey;  
  10.     NSArray *array =[[NSArray alloc] initWidthObjects:@"One", @"Two", @"Three", nil];  
  11.     //爲了演示的目的,這裏使用initWithFormat:來確保字符串可以被銷燬  
  12.     NSString * overview = [[NSString alloc] initWithFormat:@"@",@"First three numbers"];  
  13.     objc_setAssociatedObject(array, &overviewKey, overview, OBJC_ASSOCIATION_RETAIN);  
  14.     [overview release];  
  15.       
  16.     NSString *associatedObject = (NSString *)objc_getAssociatedObject(arrray, &overviewKey);  
  17.     NSLog(@"associatedObject:%@", associatedObject);  
  18.       
  19.     objc_setAssociatedObject(array, &overviewKey, nil, OBJC_ASSOCIATION_ASSIGN);  
  20.     [array release];  
  21.       
  22.     [pool drain];  
  23.     return 0;  
  24. }  


1. 給NSObject類動態添加屬性

h定義部分
  1. @interface UIWebView (LoadProgress)  
  2.   
  3. @property (nonatomic, assign) NSInteger resourceCount;  
  4.   
  5. @end  

m實現部分

首先要定義一個全局的key

  1. // resourceCount object keys  
  2. static void *s_resourceCountKey = &s_resourceCountKey;  
  3. //static void *s_resourceCountKey = "s_resourceCountKey";  

初始
  1. @implementation UIWebView (LoadProgress)  
  2. @dynamic resourceCount;  

實現
  1. #pragma mark Accessors and mutators  
  2.   
  3. - (NSInteger)resourceCount  
  4. {  
  5.     NSNumber *resourceCountNumber = objc_getAssociatedObject(self, s_resourceCountKey);  
  6.     if (! resourceCountNumber)  
  7.     {  
  8.         return 0;  
  9.     }  
  10.     else  
  11.     {  
  12.         return [resourceCountNumber integerValue];  
  13.     }  
  14. }  
  15.   
  16. - (void)setResourceCount:(NSInteger)rCount  
  17. {  
  18.     objc_setAssociatedObject(self, s_resourceCountKey, [NSNumber numberWithInteger:rCount], OBJC_ASSOCIATION_RETAIN_NONATOMIC);  
  19. }  

這樣就可以直接使用了

webView.resourceCount = 10;



2. 給NSObject類動態添加代理protocol

同屬性

動態創建代理暫時先不寫了


3. 替換或變更NSObject類方法Method

基本替換方法

  1. // 替換類方法  
  2. // frome: CoconutKit  
  3. IMP HLSSwizzleClassSelector(Class clazz, SEL selector, IMP newImplementation)  
  4. {  
  5.     // Get the original implementation we are replacing  
  6.     Class metaClass = objc_getMetaClass(class_getName(clazz));  
  7.     Method method = class_getClassMethod(metaClass, selector);  
  8.     IMP origImp = method_getImplementation(method);  
  9.     if (! origImp) {  
  10.         return NULL;  
  11.     }  
  12.       
  13.     class_replaceMethod(metaClass, selector, newImplementation, method_getTypeEncoding(method));  
  14.     return origImp;  
  15. }  
  16.   
  17. // 替換實例方法  
  18. IMP HLSSwizzleSelector(Class clazz, SEL selector, IMP newImplementation)  
  19. {  
  20.     // Get the original implementation we are replacing  
  21.     Method method = class_getInstanceMethod(clazz, selector);  
  22.     IMP origImp = method_getImplementation(method);  
  23.     if (! origImp) {  
  24.         return NULL;  
  25.     }  
  26.       
  27.     class_replaceMethod(clazz, selector, newImplementation, method_getTypeEncoding(method));  
  28.     return origImp;  
  29. }  

新方法定義

  1. // Original implementation of the methods we swizzle  
  2. static id (*s_UIWebView__identifierForInitialRequest_Imp)(id, SEL, id, id, id) = NULL;  
  3.   
  4. // Swizzled method implementations  
  5. static id swizzled_UIWebView__identifierForInitialRequest_Imp(UIWebView *self, SEL _cmd, id webView, id initialRequest, id dataSource);  

方法初始化需要寫在類方法+ (void)load中
  1. + (void)load  
  2. {  
  3.     s_UIWebView__identifierForInitialRequest_Imp = (id (*)(id, SEL, id, id, id))HLSSwizzleSelector(self, @selector(webView:identifierForInitialRequest:fromDataSource:), (IMP)swizzled_UIWebView__identifierForInitialRequest_Imp);  
  4. }  

實現部分
  1. #pragma mark Swizzled method implementations  
  2.   
  3. static id swizzled_UIWebView__identifierForInitialRequest_Imp(UIWebView *self, SEL _cmd, id webView, id initialRequest, id dataSource)  
  4. {  
  5.     // 調用原方法  
  6.     (*s_UIWebView__identifierForInitialRequest_Imp)(self, _cmd, webView, initialRequest, dataSource);  
  7.       
  8.     [self setResourceCount:self.resourceCount+1];  
  9.       
  10.     return [NSNumber numberWithInteger:self.resourceCount];  
  11. }  


4. 代理方法檢索

可直接定義到NSObject的Category中
  1. // [self implementsProtocol:@protocol(UIActionSheetDelegate)]  
  1. // frome: CoconutKit  
  2. - (BOOL)implementsProtocol:(Protocol *)protocol  
  3. {  
  4.     // Only interested in optional methods. Required methods are checked at compilation time  
  5.     unsigned int numberOfMethods = 0;  
  6.     struct objc_method_description *methodDescriptions = protocol_copyMethodDescriptionList(protocol, NO /* optional only */, YES, &numberOfMethods);  
  7.     for (unsigned int i = 0; i < numberOfMethods; ++i) {  
  8.         struct objc_method_description methodDescription = methodDescriptions[i];  
  9.         SEL selector = methodDescription.name;  
  10.         if (! class_getInstanceMethod([self class], selector)) {  
  11.             NSString *selectorString = [NSString stringWithCString:sel_getName(selector) encoding:NSUTF8StringEncoding];  
  12.             NSString *protocolName = [NSString stringWithCString:protocol_getName(protocol) encoding:NSUTF8StringEncoding];  
  13.             HLSLoggerInfo(@"Class %@ does not implement method %@ of protocol %@", [self className], selectorString, protocolName);  
  14.             selectorString = nil;               // Just to remove unused variable warnings  
  15.             protocolName = nil;  
  16.             return NO;  
  17.         }  
  18.     }  
  19.       
  20.     return YES;  



發佈了78 篇原創文章 · 獲贊 54 · 訪問量 34萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章