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