runtime作用(上)

來源:互聯網
上載者:User

標籤:

runtime的作用有四個:1.發送訊息   2.交換方法  3.動態添加方法    4.給分類添加屬性  5.字典轉模型

作用一:發送訊息

  • 方法調用的本質,就是讓對象發送訊息。
  • objc_msgSend,只有對象才能發送訊息,因此以objc開頭.
  • 使用訊息機制前提,必須匯入#import <objc/message.h>
  • 訊息機制簡單使用
  • clang -rewrite-objc main.m 查看最終產生代碼
    // 建立person對象    Person *p = [[Person alloc] init];    // 調用對象方法    [p eat];    // 本質:讓對象發送訊息    objc_msgSend(p, @selector(eat));    // 調用類方法的方式:兩種    // 第一種通過類名調用    [Person eat];    // 第二種通過類對象調用    [[Person class] eat];    // 用類名調用類方法,底層會自動把類名轉換成類對象調用    // 本質:讓類對象發送訊息    objc_msgSend([Person class], @selector(eat));

作用二:交換方法
  • 開發使用情境:系統內建的方法功能不夠,給系統內建的方法擴充一些功能,並且保持原有的功能。
  • 方式一:繼承系統的類,重寫方法.
  • 方式二:使用runtime,交換方法.
@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    // 需求:給imageNamed方法提供功能,每次載入圖片就判斷片是否載入成功。    // 步驟一:先搞個分類,定義一個能載入圖片並且能列印的方法+ (instancetype)imageWithName:(NSString *)name;    // 步驟二:交換imageNamed和imageWithName的實現,就能調用imageWithName,間接調用imageWithName的實現。    UIImage *image = [UIImage imageNamed:@"123"];}@end@implementation UIImage (Image)// 載入分類到記憶體的時候調用+ (void)load{    // 交換方法    // 擷取imageWithName方法地址    Method imageWithName = class_getClassMethod(self, @selector(imageWithName:));    // 擷取imageWithName方法地址    Method imageName = class_getClassMethod(self, @selector(imageNamed:));    // 交換方法地址,相當於交換實現方式    method_exchangeImplementations(imageWithName, imageName);}// 不能在分類中重寫系統方法imageNamed,因為會把系統的功能給覆蓋掉,而且分類中不能調用super.// 既能載入圖片又能列印+ (instancetype)imageWithName:(NSString *)name{    // 這裡調用imageWithName,相當於調用imageName    UIImage *image = [self imageWithName:name];    if (image == nil) {        NSLog(@"載入空的圖片");    }    return image;}@end

作用三:動態添加方法

  • 開發使用情境:如果一個類方法非常多,載入類到記憶體的時候也比較耗費資源,需要給每個方法產生映射表,可以使用動態給某個類,添加方法解決。
  • 經典面試題:有沒有使用performSelector,其實主要想問你有沒有動態添加過方法。
  • 簡單使用
@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    Person *p = [[Person alloc] init];    // 預設person,沒有實現eat方法,可以通過performSelector調用,但是會報錯。    // 動態添加方法就不會報錯    [p performSelector:@selector(eat)];}@end@implementation Person// void(*)()// 預設方法都有兩個隱式參數,void eat(id self,SEL sel){    NSLog(@"%@ %@",self,NSStringFromSelector(sel));}// 當一個對象調用未實現的方法,會調用這個方法處理,並且會把對應的方法列表傳過來.// 剛好可以用來判斷,未實現的方法是不是我們想要動態添加的方法+ (BOOL)resolveInstanceMethod:(SEL)sel{    if (sel == @selector(eat)) {        // 動態添加eat方法        // 第一個參數:給哪個類添加方法        // 第二個參數:添加方法的方法編號        // 第三個參數:添加方法的函數實現(函數地址)        // 第四個參數:函數的類型,(傳回值+參數類型) v:void @:對象->self :表示SEL->_cmd        class_addMethod(self, @selector(eat), eat, "[email protected]:");    }    return [super resolveInstanceMethod:sel];}@end
 

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.