Objective-c中的SEL、Method、IMP

时间:2023-01-15 18:38:26

本文想根据文档剖析下objective-c中的关于消息的几个重点概念


1.@selector()


A selector 是 an object选择执行一个方法的name,可以看作是方法名。或者when source code is compiled,用以替代方法名的unique identifier;

A selector is the name used to select a method to execute for an object, or the unique identifier that replaces the name when the source code is compiled.


A selector 本身什么也不做,它只是一个方法的标识(identifies);当源码被编译时,the compiler makes sure that selectors are unique;

结合runtime,对于一个给定的selector,A selector 的作用就像一个动态的函数指针,对于任何使用它的类来说,自动指向该method的实现;

A selector acts like a dynamic function pointer that,in conjunction with the runtime ,for a given name,automatically points to the implementation of a method appropriate for whichever class its's used with;


举例来说,假使你有一个方法run的selector ,且Dog,Athlete,ComputerSimulation这几个类都实现了run方法。那么,这几个类中的任何一个类的instance都可以用该selector来调用自己的run方法,虽然这几个类的run方法实现不同。

selector有两种初始化方法:

Compiled selectors are of type SEL

1.在编译时,使用编译指令@selector.

SEL aSelector = @selector(methodName);
2.在运行时,使用 NSSelectorFromString function.

 
 
SEL aSelector = NSSelectorFromString(@"methodName");

当你的代码发送一个到运行时才知道函数名的消息时,你可以用string 来创建一个selector;

You use a selector created from a string when you want your code to send a message whose name you may not know until runtime.

selector的用法:

你可以用performSelector及performSelector类似的方法,通过a selector来调用一个函数;

You can invoke a method using a selector with performSelector: and other similar methods.

SEL aSelector = @selector(run);
[aDog performSelector:aSelector];
[anAthlete performSelector:aSelector];
[aComputerSimulation performSelector:aSelector];

正常情况下,你可以直接调用method.在特定情况下,你可以用selector来调用,比如target-action design pattern.

2.SEL

定义:

typedef struct objc_selector *SEL;

Defines an opaque type that represents a method selector.

简单来说:SEL是@selector的类;

小结:

(1)Method selectors are used to represent the name of a method at runtime.

(2) A method selector is a C string that has been registered (or “mapped“) with the Objective-C runtime. Selectors generated by the compiler are automatically mapped by the runtime when the class is loaded.

(3)You can add new selectors at runtime and retrieve existing selectors using the function sel_registerName.

(4)When using selectors, you must use the value returned from sel_registerName or the Objective-C compiler directive @selector()

(5)You cannot simply cast a C string to SEL.



3.IMP

typedef id (*IMP)(id,SEL, ...); 


IMP就是一个指向method implementation的指针;(注:内存中,指针指向所在地址块的首位地址)

A pointer to the start of a method implementation.

id (*IMP)(id, SEL, ...)
Discussion

This data type is a pointer to the start of the function that implements the method. This function uses standard C calling conventions as implemented for the current CPU architecture. The first argument is a pointer to self (that is, the memory for the particular instance of this class, or, for a class method, a pointer to the metaclass). The second argument is the method selector. The method arguments follow.


4.Method

c++中的概念:上篇文章中介绍,每个对象都有自己的属性、行为,针对某一类对象共有且多次重复的行为,可以定义为该类对象共有的方法(Function)。

c++中函数的定义:返回结果类型 函数名(参数1,参数2,……参数n);

ios文档中Method的定义:

typedef struct objc_method *Method;

An opaque type that represents a method in a class definition.

struct objc_method {

    SEL method_name;

    char *method_types;

    IMP method_imp;

}

objc_method揭示了objective-c中method的结构:SEL类型的方法名、方法类型、方法的具体实现指针;


struct objc_method_list {

    structobjc_method_list *obsolete;

   int method_count;

#ifdef __LP64__

    int space;

#endif

    /* variablelength structure */

    structobjc_method method_list[1];

objc_method_list揭示了class中方法列表的具体结构;