swizzle交换方法名

在iOS的runtime中有交换方法名的函数,称为swizzle,以下示例将imageWithName:与imageNamed:两个方法进行了交换,这样调用系统方法imageNamed:实际调用的是imageWithName:,所有图片名称都拼接_os7,当旧项目需要更改一套图片时可以免去一个个更改。

@implementation UIImage (Extension)
/**
 *  只要分类被装载到内存中,就会调用1次
 */
+ (void)load
{
    Method otherMehtod = class_getClassMethod(self, @selector(imageWithName:));
    Method originMehtod = class_getClassMethod(self, @selector(imageNamed:));
    // 交换2个方法的实现
    method_exchangeImplementations(otherMehtod, originMehtod);
}

+ (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];//这里实际调用的时系统方法imageNamed:
    }

    if (image == nil) {
        image = [UIImage imageWithName:name];
    }
    return image;
}
@end
时间: 2024-07-29 02:50:04

swizzle交换方法名的相关文章

ThinkPHP 3.2 中获取所有函数方法名,以及注释,完整可运行

<?php namespace Home\Controller; use Common\Controller\BaseController; class AuthController extends BaseController{ /** * @cc index主页面 */ public function index(){ $modules = array('Home'); //模块名称 $i = 0; foreach ($modules as $module) { $all_controlle

HTML5 Canvas ( 扩展context(&#39;2d&#39;) ) CanvasRenderingContext2D.prototype.你的方法名

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>canvas</title> <script type="text/javascript" src="../js/jQuery.js"></script> <style type="text/css">

Android显示Log信息(带行号,类名,方法名)

package com.dylan.testlog; import android.util.Log; public class MyLogger { // private static final String TAG = "MyLogger"; public static boolean DEBUG = true; /** * 显示Log信息(带行号) * @param logLevel 1 v ; 2 d ; 3 i ; 4 w ; 5 e . * @param info 显示的

js自定义方法名

自定义方法名: <script language="javascript" type="text/javascript">window.onload = function(){ init( ); } function init(){var TestStrA = "abc";var TestStrB = "def";var TestStrC = TestStrA + TestStrB;alert(TestStrC);

.NET 中获取调用方法名

在写记录日志功能时,需要记录日志调用方所在的模块名.命名空间名.类名以及方法名,想到使用的是反射(涉及到反射请注意性能),但具体是哪一块儿还不了解,于是搜索,整理如下: 需要添加相应的命名空间: using System; using System.Diagnostics; using System.Reflection; 如果仅是获取当前方法名,可以使用如下代码: public static void WriteSysLog(int level, string content) { Metho

Yii2 获取模块名、控制器名、方法名

Yii2 获取模块名.控制器名.方法名在视图中: 模块名 $this->context->module->id 控制器名 $this->context->id 方法名 $this->context->action->id 在控制器中 模块名 Yii::$app->controller->module->id; 控制器名 Yii::$app->controller->id 方法名 Yii::$app->controller-

override(重写,覆盖) 1、方法名、参数、返回值相同。 2、子类方法不能缩小父类方法的访问权限。 3、子类方法不能抛出比父类方法更多的异常(但子类方法可以不抛出异常)。 4、存在于父类和子类之间。 5、方法被定义为final不能被重写。 overload(重载,过载) 1、参数类型、个数、顺序至少有一个不相同。 2、不能重载只有返回值不同的方法名。 3、存在于父类和子

override(重写,覆盖) 1.方法名.参数.返回值相同. 2.子类方法不能缩小父类方法的访问权限. 3.子类方法不能抛出比父类方法更多的异常(但子类方法可以不抛出异常). 4.存在于父类和子类之间. 5.方法被定义为final不能被重写. overload(重载,过载) 1.参数类型.个数.顺序至少有一个不相同.   2.不能重载只有返回值不同的方法名. 3.存在于父类和子类.同类中. 方法的重写(Overriding)和重载(Overloading)是Java多态性的不同表现. 重写(O

c# 获取方法所在的命名空间 类名 方法名

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.Reflection; namespace GetMethodNameSpace { class Program { public static string GetMethodInfo() { string str = ""; //取得当

WebService 通过POST方式访问时候,因 URL 意外地以“/方法名”结束,请求格式无法识别 解决办法

因URL意外地以“/方法名”结束,请求格式无法识别. 执行当前Web请求期间生成了未处理的异常.可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息. 解决方法:在webservice的web.config文件中的<system.web>节点下加入: <webServices> <protocols> <add name= "HttpPost"/> <add name= "HttpGet"/> &