赞
踩
运行时机制
,也就是在运行时候的一些机制,其中最主要的是消息机制。函数的调用在编译的时候会决定调用哪个函数
。动态调用过程
,在编译的时候并不能决定真正调用哪个函数,只有在真正运行的时候才会根据函数的名称找到对应的函数来调用。调用任何函数
,即使这个函数并未实现,只要声明过就不会报错。未实现的函数
就会报错。消息机制
前提,必须导入#import <objc/message.h>- @implementation ViewController
-
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
-
- Person *p = [[Person alloc] init];
-
- // 默认person,没有实现eat方法,可以通过performSelector调用,但是会报错。
- // 动态添加方法就不会报错
- [p performSelector:@selector(eat)];
-
- }
-
-
- @end
-
-
- @implementation Person
- // void(*)()
- // 默认方法都有两个隐式参数,
- void eat(id self,SEL sel)
- {
- NSLog(@"%@ %@",self,NSStringFromSelector(sel));
- }
-
- // 当一个对象调用未实现的方法,会调用这个方法处理,并且会把对应的方法列表传过来.
- // 刚好可以用来判断,未实现的方法是不是我们想要动态添加的方法
- + (BOOL)resolveInstanceMethod:(SEL)sel
- {
-
- if (sel == @selector(eat)) {
- // 动态添加eat方法
-
- // 第一个参数:给哪个类添加方法
- // 第二个参数:添加方法的方法编号
- // 第三个参数:添加方法的函数实现(函数地址)
- // 第四个参数:函数的类型,(返回值+参数类型) v:void @:对象->self :表示SEL->_cmd
- class_addMethod(self, @selector(eat), eat, "v@:");
-
- }
-
- return [super resolveInstanceMethod:sel];
- }
- @end
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- @implementation ViewController
-
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
-
- // 给系统NSObject类动态添加属性name
-
- NSObject *objc = [[NSObject alloc] init];
- objc.name = @"名字";
- NSLog(@"%@",objc.name);
-
- }
-
-
- @end
-
-
- // 定义关联的key
- static const char *key = "name";
-
- @implementation NSObject (Property)
-
- - (NSString *)name
- {
- // 根据关联的key,获取关联的值。
- return objc_getAssociatedObject(self, key);
- }
-
- - (void)setName:(NSString *)name
- {
- // 第一个参数:给哪个对象添加关联
- // 第二个参数:关联的key,通过这个key获取
- // 第三个参数:关联的value
- // 第四个参数:关联的策略
- objc_setAssociatedObject(self, key, name, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- }
-
- @end
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- @implementation ViewController
-
-
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- // 需求:给imageNamed方法提供功能,每次加载图片就判断下图片是否加载成功。
- // 步骤一:先搞个分类,定义一个能加载图片并且能打印的方法+ (instancetype)imageWithName:(NSString *)name;
- // 步骤二:交换imageNamed和imageWithName的实现,就能调用imageWithName,间接调用imageWithName的实现。
- UIImage *image = [UIImage imageNamed:@"123"];
-
- }
-
- @end
-
-
- @implementation UIImage (Image)
- // 加载分类到内存的时候调用
- + (void)load
- {
- // 交换方法
-
- // 获取imageWithName方法地址
- Method imageWithName = class_getClassMethod(self, @selector(imageWithName:));
-
- // 获取imageWithName方法地址
- Method imageName = class_getClassMethod(self, @selector(imageNamed:));
-
- // 交换方法地址,相当于交换实现方式
- method_exchangeImplementations(imageWithName, imageName);
-
-
- }
-
- // 不能在分类中重写系统方法imageNamed,因为会把系统的功能给覆盖掉,而且分类中不能调用super.
-
- // 既能加载图片又能打印
- + (instancetype)imageWithName:(NSString *)name
- {
-
- // 这里调用imageWithName,相当于调用imageName
- UIImage *image = [self imageWithName:name];
-
- if (image == nil) {
- NSLog(@"加载空的图片");
- }
-
- return image;
- }
-
-
- @end
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。