美文网首页
Type Encodings

Type Encodings

作者: CharmecarWang | 来源:发表于2020-03-01 17:15 被阅读0次

Type Encodings

在使用消息转发的时候,我们经常会用到到这个方法methodSignatureForSelector

- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
{
    if (self.target == nil)
        return [NSMethodSignature signatureWithObjCTypes:"v@:@"];
    return [self.target methodSignatureForSelector:selector];
}

该方法用来返回方法签名,即方法的返回值和参数。

那这里的v@:@是什么意思呢?
在OC中方法调用,又称为向一个对象发送消息,比如:

[receiver message]

编译后就是这个样子:

objc_msgSend(receiver, selector)

objc_msgSend方法的文档:

id objc_msgSend(id self, SEL op, ...)

参数:

  • self 消息的接收者
  • op 消息的selector
  • … 消息传入参数的数组

这里的 “v@:@”就代表:
"v":代表返回值void,其实就对应着objc_msgSend的返回值
"@":代表一个id类型的对象,也就是消息的receiver
":":代表SEL
"@":代表参数,该参数是个id类型对象

再看一个例子

- (BOOL)hasLoadData:(NSString *)url

其ObjCTypes为:"B@:@":

  • "B":代表返回值是BOOL
  • "@":代表一个id类型的对象,也就是消息的receiver
  • ":":代表SEL
  • "@":代表参数,该参数是个id类型对象,也就是url

那么我们是怎么确定这个ObjCTypes的呢?

  • 通过encode() 函数,// NSLog(@"%s",@encode(BOOL))的结果为B
  • 看官方文档Type Encodings,这里面列了具体的对应关系

相关文章

  • iOS 的Type Encodings

    iOS 的Type Encodings Type Encodings compiler 编码encode 每个me...

  • iOS 类型编码(Type Encodings)

    Type Encodings

  • Type Encodings

    Type Encodings Apple Type Encodings 就是编译器把 method 的参数列表和返...

  • Type Encodings

    为了帮助运行时系统,编译器将每个方法中的返回值类型和参数类型进行编码,并将方法名和方法选择器进行关联。编译器通过@...

  • Type Encodings

    @encode,@编译器指令 之一,返回一个给定类型编码为一种内部表示的字符串(例如,@encode(int) →...

  • Type Encodings

    Type Encodings 在使用消息转发的时候,我们经常会用到到这个方法methodSignatureForS...

  • Runtime:Type Encodings

    序言:编译器把每个方法的返回类型和参数类型编码成跟该方法相关联的字符,同时编码器也对外提供了获取相应编码字符的指令...

  • Type Encodings 、NSMethodSignatur

    前言 一般来说,写东西都是由上而下顶层联想,由于这次是知识总结,准备从浅入深,一点一点写下去。文章段落之间好想关系...

  • Runtime - Method

    参考文档 Type Encodings[https://developer.apple.com/library/a...

  • Runtime(四) 函数返回值和参数类型编码

    Type Encodings 为了协助(assist)runtime system,编译器(compiler)会把...

网友评论

      本文标题:Type Encodings

      本文链接:https://www.haomeiwen.com/subject/uwxhkhtx.html