交换方法

//

//ViewController.m

//CocoTest_1

//

//Created by S u p e r m a n on 2017/3/14.

//Copyright © 2017年张浩. All rights reserved.

//

#import"ViewController.h"

#import"UIImage+AddImageFunc.h"

#import

@implementationUIImage (AddImageFunc)

+ (void)load {

MethodmethodOfOrigin =class_getClassMethod(self,@selector(imageNamed:));

MethodmethodOfCurrent=class_getClassMethod(self,@selector(custom_imageNamed:));

//交换两个方法

method_exchangeImplementations(methodOfOrigin, methodOfCurrent);

}

/*

*外界主动调用imageNamed:的时候,相当于调用了custom_imageNamed:这个方法

*

**/

+ (UIImage*)custom_imageNamed:(NSString*)name {

if(name.length==0) {

name =@"1";

}

/*

*1.当主动调用[self custom_imageNamed:name]

2.因为已经交换了所以相当于调用[self imageNamed:name];

**/

UIImage* image = [selfcustom_imageNamed:name];

returnimage;

returnnil;

}

@end

你可能感兴趣的:(交换方法)