一、NSMethodSignature
+ (nullable NSMethodSignature *)signatureWithObjCTypes:(const char *)types;
//类方法 唯一初始化方法 对指定方法进行签名
//types 参数C字符串,
Code | Meaning |
---|---|
| A |
| An |
| A |
| A
|
| A |
| An |
| An |
| An |
| An |
| An |
| A |
| A |
| A C++ |
| A |
| A character string ( |
| An object (whether statically typed or typed |
| A class object ( |
| A method selector ( |
[array type] | An array |
{name=type...} | A structure |
(name=type...) | A union |
| A bit field of num bits |
| A pointer to type |
| An unknown type (among other things, this code is used for function pointers) |
具体可见 https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html
//规则: 返回值 函数名: 具体参数个数 如 (NSString*)fun1:(NSString*)param1对应 “@@:@”,打印如下
<NSMethodSignature: 0x7a2dc790>
number of arguments = 3
frame size = 12
is special struct return? NO
return value: -------- -------- -------- --------
type encoding (@) '@'
flags {isObject}
modifiers {}
frame {offset = 0, offset adjust = 0, size = 4, size adjust = 0}
memory {offset = 0, size = 4}
argument 0: -------- -------- -------- --------
type encoding (@) '@'
flags {isObject}
modifiers {}
frame {offset = 0, offset adjust = 0, size = 4, size adjust = 0}
memory {offset = 0, size = 4}
argument 1: -------- -------- -------- --------
type encoding (:) ':'
flags {}
modifiers {}
frame {offset = 4, offset adjust = 0, size = 4, size adjust = 0}
memory {offset = 0, size = 4}
argument 2: -------- -------- -------- --------
type encoding (@) '@'
flags {isObject}
modifiers {}
frame {offset = 8, offset adjust = 0, size = 4, size adjust = 0}
memory {offset = 0, size = 4}
@property (readonly) NSUInteger numberOfArguments;
//参数个数
//取参数类型
@property (readonly) NSUInteger frameLength;
- (BOOL)isOneway;
@property (readonly) const char *methodReturnType NS_RETURNS_INNER_POINTER;
//返回值类型
@property (readonly) NSUInteger methodReturnLength;
//返回长度
二、NSInvocation对原来签名的方法执行新的方法,必须指定Selector和Target,invoke或invokeWithTarget执行
+ (NSInvocation *)invocationWithMethodSignature:(NSMethodSignature *)sig;
//类方法 生成签名方法实例
@property (readonly, retain) NSMethodSignature *methodSignature;
//返回包装的签名方法
- (void)retainArguments;
@property (readonly) BOOL argumentsRetained;
@property (nullable, assign) id target;
//返回对象
@property SEL selector;
//返回包装的SEL
- (void)getReturnValue:(void *)retLoc;
//返回签名方法的返回值
- (void)setReturnValue:(void *)retLoc;
//设置签名方法的返回值
- (void)getArgument:(void *)argumentLocation atIndex:(NSInteger)idx;
//返回签名方法的参数 index=2,取第一个参数
- (void)setArgument:(void *)argumentLocation atIndex:(NSInteger)idx;
//设置签名方法参数
- (void)invoke;
//执行方法
- (void)invokeWithTarget:(id)target;
//指定对象执行 相当于 setTarget和invoke
三 、NSObject类的方法
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector //中进行未定义方法的封装
- (void)forwardInvocation:(NSInvocation *)anInvocation;//对封装的方法进行调用