你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> Runtime(交流辦法)筆記

Runtime(交流辦法)筆記

編輯:IOS開發綜合

Runtime是在不得不必時才停止運用的,普通狀況下最好不要運用。Runtime的交流辦法普通用於交換零碎自帶的辦法,比方處置數組越界問題、判別圖片能否加載成功,等等。本帖的舉例闡明判別圖片能否加載成功。

ViewController.m
- (void)viewDidLoad {
    [super viewDidLoad];

    //image的圖片是隨意起的名字,不存在圖片
    UIImage *image = [UIImage imageNamed:@"1"];

    //這張是存在的
    UIImage *image2 = [UIImage imageNamed:@"RadioHead"];
}
這裡我們要運用自定義辦法對UIImage的-imageNamed:辦法停止交流,我們需求創立一個UIImage的類別(Category)UIImage+Image
#import "UIImage+Image.h"
#import <objc/message.h>//這裡用於引入Runtime

@implementation UIImage (Image)
+(void)load{
    Method method1 = class_getClassMethod([UIImage class], @selector(imageNamed:));
    Method method2 = class_getClassMethod([UIImage class], @selector(tw_imageNamed:));
    method_exchangeImplementations(method1, method2);
}

//自定義辦法
+(UIImage *)tw_imageNamed:(NSString *)name{
    UIImage *image = [UIImage tw_imageNamed:name];
    if (image) {
        NSLog(@"加載圖片成功: FUNCTION:%s;imageName:\"%@\"",__FUNCTION__,name);
    }else{
        NSLog(@"正告!加載圖片失敗:FUNCTION:%s;imageName:\"%@\"",__FUNCTION__,name);
    }

    return image;
}

@end
運轉效果
Runtime(交換方法)筆記

【Runtime(交流辦法)筆記】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved