NSInvocation
NSInvocation本質(zhì)就是將一個(gè)方法轉(zhuǎn)發(fā)成一個(gè)對(duì)象斟湃。NSInvocation中的selector 直接關(guān)聯(lián)著一個(gè)方法
@interface NSInvocation:NSObject
通過(guò)方法簽名獲得NSInvocation對(duì)象
-
(NSInvocation)invocationWithMethodSignation:(NSMethodSignature)sig;
readonly的方法簽名對(duì)象。只可以在實(shí)例化對(duì)象的時(shí)候傳入妓肢,之后只能讀取不可以修改
@property (readonly, retain) NSMethodSignature *methodSignature;
//可以設(shè)置的調(diào)用對(duì)象
@property(nullable,assign) id target;// 調(diào)用方法
@property SEL selector;
//調(diào)用
-(void)invoke;
//讓某個(gè)對(duì)象去相應(yīng)
-(void)invokeWithTarget:(id)target;
NSMethodSignature(方法簽名)
主要方法是 (nullable NSMethodSignature *)signatureWithObjCTypes:(const char *)types;
通過(guò)一個(gè)Type Encoding 來(lái)實(shí)例化一個(gè)方法簽名的實(shí)例對(duì)象疆柔。
Type Encoding一般格式為"v@:","v@:@","v@:i"等晴音。不同的字符代表不同的值
runtime的一些APi
Class object_getClass
struct objc_method_description protocol_getMethodDescription(Protocol * _Nonnull proto, SEL _Nonnull aSel, BOOL isRequiredMethod, BOOL isInstanceMethod)
獲取協(xié)議中某個(gè)方法的聲明灰粮。獲得是一個(gè)結(jié)構(gòu)體對(duì)象炬搭。 objc_method_description 中包含一個(gè)SEL對(duì)象夺姑,和一個(gè)const char* type對(duì)象protocol_isEqual(Protocol * _Nullable proto, Protocol * _Nullable other) 比較兩個(gè)協(xié)議是否相同
sel_isEqual(SEL _Nonnull lhs, SEL _Nonnull rhs)
比較2個(gè)Selector是否相同obj_getProtcol(const char*) 通過(guò)一個(gè)字符串得到一個(gè)Protocol
得到一個(gè)類(lèi)實(shí)例的父類(lèi) class_getSuperClass(Class class);