首页 技术 正文
技术 2022年11月12日
0 收藏 741 点赞 4,474 浏览 1199 个字
动态加载
#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:@];}- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.}Person.h
#import<Foundation/Foundation.h>@interfacePerson :NSObject@endPerson.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

源自小马哥教学视频

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290