zoukankan      html  css  js  c++  java
  • iOS 的 runtime

    Runtime

    运行时的使用:

    1. KVO , KVC 

    2. 运行过程中交换两个方法的实现,改系统的方法.

      例如:  当一个做了几年的项目要从iOS6适配到iOS7时,要把之前的图片全部换掉,可通过扩展UIImage 实现它的分类.补充一个类方法imageWithName: name.然后将系统的imageName:方法与imageWithName:name 的方法在运行时换掉,而不用修改其他代码.

    #import "UIImage+Extension.h"
    #import <objc/runtime.h>
    
    @implement UIImage(Extension)
    
    +(void) load
    {
        Method otherMethod = class_getClassMethod([UIImage class],@selector(imageWithName:));
        Method originMethod = class_getClassMethod([UIImage class],@selector(imageNamed:));
        //交换两个方法的实现
        method_exchangeImplementations(otherMethod, originMethod);
    }
    
    + (UIImage *) imageWithName:(NSString *) name
    {
        BOOL ios7 = [[UIDevice currentDevice].systemVersion floatValue] >= 7.0;
        UIImage * image = nil;
        if (ios7)
        {
            NSString * newName = [name stringByAppendingString:@"_os7"];
            image = [UIImage imageWithName:newName];
            }
        if (image ==nil)
        {
            image = [UIImage imageWithName: name];
        }
        return image;
    }
    @end
  • 相关阅读:
    78. Subsets java solutions
    77. Combinations java solutions
    236. Lowest Common Ancestor of a Binary Tree java solutions
    86. Partition List java solutions
    39. Combination Sum java solutions
    129. Sum Root to Leaf Numbers java solutions
    杭电1004
    杭电1003
    杭电1002
    杭电1001
  • 原文地址:https://www.cnblogs.com/aunty/p/5141964.html
Copyright © 2011-2022 走看看