zoukankan      html  css  js  c++  java
  • Runtime 动态加载方法

    动态加载
    #import"ViewController.h"
    #import"Person.h"
    
    @interfaceViewController()
    
    @end
    
    @implementationViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
       // performSelector:动态添加方法
       Person*p = [[Person alloc]init];
    
       //动态添加方法
       //[p performSelector:@selector(eat)];

    //传参 [pperformSelector:@selector(eat:)withObject:@111]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } Person.h #import<Foundation/Foundation.h> @interfacePerson :NSObject @end Person.m #import"Person.h" #import<objc/message.h> @implementationPerson //定义函数 //没有返回值,参数(id,SEL) // void(id,SEL) voidaaaa(idself,SEL_cmd,idparam1) { NSLog(@"调用eat %@ %@ %@",self,NSStringFromSelector(_cmd),param1); } //默认一个方法都有两个参数,self,_cmd,隐式参数 // self:方法调用者 // _cmd:调用方法的编号 //动态添加方法,首先实现这个resolveInstanceMethod // resolveInstanceMethod调用:当调用了没有实现的方法没有实现就会调用resolveInstanceMethod // resolveInstanceMethod作用:就知道哪些方法没有实现,从而动态添加方法 // sel:没有实现方法 + (BOOL)resolveInstanceMethod:(SEL)sel { // NSLog(@"%@",NSStringFromSelector(sel)); //动态添加eat方法 if(sel ==@selector(eat:)) { /* cls:给哪个类添加方法 SEL:添加方法的方法编号是什么 IMP:方法实现,函数入口,函数名 types:方法类型 */ // @:对象:SEL class_addMethod(self, sel, (IMP)aaaa,"v@:@");//V@:@所代表的意思请查阅官方文档 //处理完 return YES; } return[super resolveInstanceMethod:sel]; } @end

    源自小马哥教学视频

  • 相关阅读:
    GitLab 介绍
    git 标签
    git 分支
    git 仓库 撤销提交 git reset and 查看本地历史操作 git reflog
    git 仓库 回退功能 git checkout
    python 并发编程 多进程 练习题
    git 命令 查看历史提交 git log
    git 命令 git diff 查看 Git 区域文件的具体改动
    POJ 2608
    POJ 2610
  • 原文地址:https://www.cnblogs.com/mapanguan/p/5498173.html
Copyright © 2011-2022 走看看