1.使用消息發(fā)送機(jī)制創(chuàng)建對(duì)象亭珍,給對(duì)象發(fā)送消息
導(dǎo)入 #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里面設(shè)置 輸入msg修改為NO不檢測(cè)調(diào)用方法是用運(yùn)行時(shí),蘋(píng)果不建議直接使用objc_message來(lái)發(fā)送消息
2. runTime方法交換的使用
#import "NSURL+NewRequestUrl.h" //URL分類
#import <objc/runtime.h>
@implementation NSURL (NewRequestUrl)
//當(dāng)項(xiàng)目所有.m文件裝載到內(nèi)存優(yōu)先執(zhí)行l(wèi)oad里面的指令欺劳,所以app啟動(dòng)之前就已經(jīng)執(zhí)行了load
+(void) load{
// Method instanceMethod = class_getInstanceMethod([<#Class _Nullable __unsafe_unretained cls#>], <#SEL _Nonnull name#>)//對(duì)象方法
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已經(jīng)入這個(gè)控制器,調(diào)用URLWithString方法時(shí)航夺,就交換,最大的好處如果原工程相同方法比較多,又需要修改崔涂,可以不改變?cè)こ痰倪@些方法法阳掐,新增一個(gè)方法,避免去過(guò)多修改別人代碼導(dǎo)致混亂冷蚂,直接修改全局缭保,減少工作量!
- (void)viewDidLoad {
[super viewDidLoad];
/**
oc: 方法實(shí)質(zhì)上就是 Sel指針指向 ---> IMP指針 --->IMP指向具體的方法實(shí)現(xiàn)代碼
*/
NSURL * url = [NSURL URLWithString:@"http://www.baidu.com百度"];
NSURLRequest * request = [NSURLRequest requestWithURL:url];
NSLog(@"%@",request);
}
3. KVO本質(zhì)其實(shí)也是runtime
#import "ViewController.h"
#import "Person.h"
@interface ViewController ()
@end
@implementation ViewController{
Person * p;
}
- (void)viewDidLoad {
[super viewDidLoad];
//KVO 底層原理也是應(yīng)用運(yùn)行時(shí)
p =[[Person alloc] init];
//1.動(dòng)態(tài)創(chuàng)建Person子類(NSKVONotyfing_person)2.改變p對(duì)象的類型為(NSKVONotyfing_person)
[p addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil];
}
//KVO是對(duì)象屬性的setter方法監(jiān)聽(tīng)蝙茶,所以對(duì) 對(duì)象的成員屬性的監(jiān)聽(tīng)是不會(huì)走 -(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";
}
// 切記切記:在使用了鍵值觀察者模式一定要在移除艺骂,不然會(huì)發(fā)生崩潰
-(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調(diào)用根據(jù)添加觀察者時(shí)設(shè)置的NSKeyValueObservingOptionNew(改變以后在調(diào)用)值 NSKeyValueObservingOptionOld(改變之前調(diào)用)
[self willChangeValueForKey:name];
[super setName:name];
[self didChangeValueForKey:name];
}
@end
屏幕快照 2018-03-08 下午3.37.52.png
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者