Objective-C Runtime能做什嗎?

來源:互聯網
上載者:User

標籤:

在之前的文章中我們介紹了Runtime是什麼,屬於理論性介紹,你看了上篇很迫切的想知道Runtime到底能幹什嗎?不要著急,這一篇Blog將將講解Runtime怎麼應用到實戰中Runtime官方文檔在這裡,包括了介面名字以及使用說明。下文講到的介面都能在此文檔中找到。

  KVC中setValue中使用我們知道在KVC中如果直接setValue如果對象沒有這個屬性或者是變數就會直接Crash,如:
  1. RuntimeObj *obj = [[RuntimeObj alloc]init]; 
  2. [obj setValue:@"value4Name" forKey:@"objName"];//RuntimeObj 沒有objName這個屬性 
這段代碼會直接Crash 有沒有對這個感覺頭疼,要是能先用某種方式檢查下再set那就不會Crash? 沒錯,這件事情Runtime能做到 先看一下範例程式碼吧:
  1. -(BOOL)hasAttribute:(NSString *)attName 
  2.     BOOL flag = NO; 
  3.     u_int               count; 
  4.     Ivar *ivars = class_copyIvarList([self class], &count); 
  5.     for (int i = 0; i < count ; i++) 
  6.     { 
  7.         const char* propertyName = ivar_getName(ivars[i]); 
  8.         NSString *strName = [NSString  stringWithCString:propertyName encoding:NSUTF8StringEncoding]; 
  9.         if ([attName isEqualToString:strName]) { 
  10.             flag = YES; 
  11.         } 
  12.         NSLog(@"===%@",strName); 
  13.     } 
  14.     return flag; 
沒錯,這個函數就是能幫你檢查是否有某個屬性或變數,下面講解下一個代碼:*  Ivar 原型是 typedef struct objc_ivar *Ivar;  *  class_copyIvarList 返回的是某個類所有屬性或變數原型 Ivar *class_copyIvarList(Class cls, unsigned int *outCount) ; *  ivar_getName 返回的是沒有 Ivar 結構體的名字,即變數的名字 原型  const char *ivar_getName(Ivar v); * 與這個對應的還有一個函數 class_copyPropertyListclass_copyIvarList 不同點在前者只取屬性( @property申明的屬性) 後者所有的 包括在interface大括弧中申明的。 class_copyPropertyList使用的範例程式碼如下:
  1. objc_property_t*    properties= class_copyPropertyList([self class], &count); 
  2. for (int i = 0; i < count ; i++) 
  3.      const char* propertyName = property_getName(properties[i]); 
  4.      NSString *strName = [NSString  stringWithCString:propertyName encoding:NSUTF8StringEncoding]; 
  5.       NSLog(@"===%@",strName); 
  兩個不同可以用代碼來示範的,具體代碼自己動手寫 我就不貼出來,看看兩者到底有什麼區別? 有了這一步 你還擔心濫用KVC時崩潰了嗎?  動態建立函數有時候會根據項目需求動態建立某個函數,沒錯Runtime完全能做到 先看代碼:
  1. void dynamicMethod(id self, SEL _cmd) 
  2.     printf("SEL %s did not exist\n",sel_getName(_cmd)); 
  3.   
  4. + (BOOL) resolveInstanceMethod:(SEL)aSEL 
  5.     
  6.     class_addMethod([self class], aSEL, (IMP)dynamicMethod, "[email protected]:"); 
  7.     return YES; 
  8. void dynamicMethod(id self, SEL _cmd) 
  9.     printf("SEL %s did not exist\n",sel_getName(_cmd)); 
  10.   
  11. + (BOOL) resolveInstanceMethod:(SEL)aSEL 
  12.     class_addMethod([self class], aSEL, (IMP)dynamicMethod, "[email protected]:"); 
  13.     return YES; 
 測試代碼:
  1. RuntimeObj *obj = [[RuntimeObj alloc]init]; 
  2. [obj performSelector:@selector(dynamicMethod:)]; 
 看看代碼運行效果 講解:  * + (BOOL) resolveInstanceMethod:(SEL)aSEL 是在調用此類方法時,如果沒有這個方法就會掉這個函數。  * class_addMethod 就是動態給類添加方法 原型  BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)   
 註:IMP 是函數指標
 * “ [email protected]:” 是參數的一種寫法 以後會做詳細講解   替換已有函數在混合編碼的時候不能按照已有思路執行原來的函數,那我們把它替換掉不就好了嘛,看Runtime是怎麼做到的? 先上代碼:(注講下面的代碼是為了講targetReplaceMethod 替換成 demoReplaceMethod)
  1. void demoReplaceMethod(id SELF, SEL _cmd) 
  2.     NSLog(@"demoReplaceMethod"); 
  3.   
  4. -(void)replaceMethod 
  5.     Class strcls = [self class]; 
  6.     SEL  targetSelector = @selector(targetRelplacMethod); 
  7.     class_replaceMethod(strcls,targetSelector,(IMP)demoReplaceMethod,NULL); 
  8.   
  9. -(void)targetRelplacMethod 
  10.     NSLog(@"targetRelplacMethod"); 
 測試代碼:
  1. RuntimeObj *obj = [[RuntimeObj alloc]init]; 
  2. [obj replaceMethod]; 
  3. [obj targetRelplacMethod]; 
 運行結果:
  1. 2014-05-12 19:38:37.490 Runtime[1497:303] demoReplaceMethod 
 是不是原來的   NSLog(@”targetRelplacMethod”); 這句話就沒有執行 被替換掉了! 註:1.  class_replaceMethod 方法就是動態替換 Method的函數,原型  IMP 。2.  class_replaceMethod(Class cls, SEL name,IMP imp, const char *types) 傳回值就是一個新函數的地址( IMP指標)。3. 在實際項目中會經常用到這種方式, 比如:iOS 7以及7以下繪製NavigationBar, 自己慢慢體會吧。  動態掛載對象掛載這個詞語大家應該並不陌生吧,但是在這裡有一點點微妙的不同,在這裡博主也不是很好解釋這個詞語到底什麼含義,那我來舉個例子吧 如:如果你在對象傳遞(傳參)的時候需要用到某個屬性,按照以往的思路:我繼承這個類重新一個新類就完事了,OK,這個思路沒有問題,但是你不覺得要建立一個.h和一個.m檔案有點麻煩?程式員都是懶惰的,要是有一個方法能直接講我想要的屬性掛載上前去豈不是更好?代碼簡單、易懂。看了標題你就應該知道Runtime能幫你實現你的願望。 下面就來講解下如何使用Runtime來 在已有對象上動態掛載另外一個對象。 先不說 直接放代碼(這裡以UIAlertView為例子):
  1. //掛載對象所需要的參數(UIAlertView掛載對象) 
  2. static const char kRepresentedObject; 
  3. -(void)showAlert:(id)sender 
  4.     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去看看", nil]; 
  5.     alert.tag = ALERT_GOTO_TAG; 
  6.     objc_setAssociatedObject(alert, &kRepresentedObject, 
  7.     @"我是被掛載的", 
  8.     OBJC_ASSOCIATION_RETAIN_NONATOMIC); 
  9.     [alert show]; 
 這個只是掛載看看如何去擷取我們掛載的對象(NSString @“我是被掛載的”)
  1. -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
  2.     if (buttonIndex == 1) { 
  3.         NSString *str = objc_getAssociatedObject(alertView, 
  4.                                                     &kRepresentedObject); 
  5.         NSLog(@"%@",str) 
  6.     }  
 自己動手編寫代碼看看效果 是不是和你想的一樣? 下面講解下:1.  static const char kRepresentedObject; 這個只是一個標記,但是必不可少 具體什麼作用沒做過調研,我覺得應該就是你掛載的一個標記  Runtime 應該會根據這個標記來區別被掛載對象是掛載在哪個執行個體上。2.  objc_setAssociatedObject 動態設定關聯對象(也就是掛載)。3.  objc_getAssociatedObject 動態擷取關聯對象 看到沒有這裡也要傳  kRepresentedObject 這個標記,好像有點證明我前面的猜想了。

Objective-C Runtime能做什嗎?

聯繫我們

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