五、實例方法去哪裏找?git
/** * Adds a new method to a class with a given name and implementation. * * @param cls The class to which to add a method. * @param name A selector that specifies the name of the method being added. * @param imp A function which is the implementation of the new method. The function must take at least two arguments—self and _cmd. * @param types An array of characters that describe the types of the arguments to the method. * * @return YES if the method was added successfully, otherwise NO * (for example, the class already contains a method implementation with that name). * * @note class_addMethod will add an override of a superclass's implementation, * but will not replace an existing implementation in this class. * To change an existing implementation, use method_setImplementation. */
OBJC_EXPORT BOOL class_addMethod(Class cls, SEL name, IMP imp,
const char *types)
__OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
複製代碼
const char *types含義表:
github
Code | Meaning |
---|---|
c | A char |
i | An int |
s | A short |
l | A long l is treated as a 32-bit quantity on 64-bit programs. |
q | A long long |
C | An unsigned char |
I | An unsigned int |
S | An unsigned short |
L | An unsigned long |
Q | An unsigned long long |
f | A float |
d | A double |
B | A C++ bool or a C99 _Bool |
v | A void |
* | A character string (char *) |
@ | An object (whether statically typed or typed id) |
# | A class object (Class) |
: | A method selector (SEL) |
[array type] | An array |
{name=type...} | A structure |
(name=type...) | A union |
bnum | A bit field of num bits |
^type | A pointer to type |
? | An unknown type (among other things, this code is used for function pointers) |
注意:
用這個方法添加的方法是沒法直接調用的,必須用performSelector:調用。 由於performSelector是運行時系統負責去找方法的,在編譯時候不作任何校驗;若是直接調用編譯是會自動校驗。 添加方法是在運行時添加的,你在編譯的時候尚未這個本類方法,因此固然不行。數組
代碼架構
類方法: app
實例方法: ide
詳細流程: 函數
感謝大佬提供的圖片。ui
本篇相關代碼this
郵箱: adrenine@163.com
spa
郵箱: holaux@gmail.com
郵箱: ledahapple@icloud.com