RunTime

1.使用消息发送机制创建对象,给对象发送消息
导入 #import <objc/message.h>
  /* - OC
    Person * person = [Person new];
    [person run];
     */
    Person * person = objc_msgSend(objc_getClass("Person"), sel_registerName("alloc"));
    objc_msgSend(person, sel_registerName("init"));
    objc_msgSend(person, @selector(run));//需要在Build setting里面设置 输入msg修改为NO不检测调用方法是用运行时,苹果不建议直接使用objc_message来发送消息
2. runTime方法交换的使用
#import "NSURL+NewRequestUrl.h" //URL分类
#import <objc/runtime.h>

@implementation NSURL (NewRequestUrl)
//当项目所有.m文件装载到内存优先执行load里面的指令,所以app启动之前就已经执行了load
+(void) load{
   
//    Method instanceMethod = class_getInstanceMethod([<#Class  _Nullable __unsafe_unretained cls#>], <#SEL  _Nonnull name#>)//对象方法
   
   Method classSystemMethod = class_getClassMethod(self, @selector(URLWithString:));
   Method classCustomMethod = class_getClassMethod(self, @selector(AwNewReqestUrlWithString:));
   method_exchangeImplementations(classCustomMethod, classSystemMethod);
}

+(instancetype) AwNewReqestUrlWithString:(NSString *) urlSting{
   
   NSURL * url = [NSURL AwNewReqestUrlWithString:urlSting];
   if ([url isKindOfClass:[NSNull class]] || url == nil || url.description.length == 0) {
       NSLog(@"url为nil");
   }
   return url;
}
@end
//app已经入这个控制器,调用URLWithString方法时,就交换,最大的好处如果原工程相同方法比较多,又需要修改,可以不改变原工程的这些方法法,新增一个方法,避免去过多修改别人代码导致混乱,直接修改全局,减少工作量!
- (void)viewDidLoad {
    [super viewDidLoad];
    
    /**
     oc: 方法实质上就是 Sel指针指向 ---> IMP指针  --->IMP指向具体的方法实现代码
     */
    NSURL * url = [NSURL URLWithString:@"http://www.baidu.com百度"];
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    NSLog(@"%@",request);
    
}
3. KVO本质其实也是runtime
#import "ViewController.h"
#import "Person.h"
@interface ViewController ()

@end

@implementation ViewController{
    Person * p;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //KVO 底层原理也是应用运行时
    p  =[[Person alloc] init];
    //1.动态创建Person子类(NSKVONotyfing_person)2.改变p对象的类型为(NSKVONotyfing_person)
    [p addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil];
}
//KVO是对象属性的setter方法监听,所以对 对象的成员属性的监听是不会走 -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context方法
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    NSLog(@"%@---%@---%@",keyPath,object,change);
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    p.name = @"awuge";
}

// 切记切记:在使用了键值观察者模式一定要在移除,不然会发生崩溃
-(void)dealloc{
    [p removeObserver:self forKeyPath:@"name"];
}
#import <Foundation/Foundation.h>

@interface Person : NSObject
@property(nonatomic,strong) NSString * name;
@end
-------------------

#import "Person.h"

@implementation Person

@end


#import "Person.h"

@interface NSKVONotyfing_person : Person

@end

-----------------------------------

#import "NSKVONotyfing_person.h"

@implementation NSKVONotyfing_person

-(void)setName:(NSString *)name{
    //willChangeValueForKey和didChangeValueForKey调用根据添加观察者时设置的NSKeyValueObservingOptionNew(改变以后在调用)值 NSKeyValueObservingOptionOld(改变之前调用)
    [self willChangeValueForKey:name]; 
    [super setName:name];
    [self didChangeValueForKey:name];
}
@end
屏幕快照 2018-03-08 下午3.37.52.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Runtime是什么 Runtime 又叫运行时,是一套底层的 C 语言 API,其为 iOS 内部的核心之一,我...
    SuAdrenine阅读 4,415评论 0 3
  • 对于从事 iOS 开发人员来说,所有的人都会答出【runtime 是运行时】什么情况下用runtime?大部分人能...
    梦夜繁星阅读 9,111评论 7 64
  • 这篇文章完全是基于南峰子老师博客的转载 这篇文章完全是基于南峰子老师博客的转载 这篇文章完全是基于南峰子老师博客的...
    西木阅读 30,745评论 33 466
  • 五堡魔鬼城距离哈密市百余公里,属于雅丹地貌特征,石头牌上写着:自然景观,天下第一。也不知道遇见过多少个第一,这又是...
    传说中的超超阅读 3,229评论 3 8
  • 晨起三更,跑了趟高铁站。城市的街道,没有了白天的拥挤和喧嚣,显出难得的空旷和静默,驾车疾驰的感觉爽极了。 ...
    海月先生阅读 3,984评论 0 6

友情链接更多精彩内容