Objective-C type encodings runtime

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)
void test(id self,SEL __cmd)
{

}```
函数参数类型: "v@:"

=================================

oc语言相关的都是Objc_开头

objc_getMetaClass(class_getName(cls))...

=================================

对象相关的object_开发

object_setClass...

=================================

类相关的操作都是class_开头的

class_getName()...

成员变量,属性,成员函数,都是类相关的都可以用class_get***获得对应的指针;

也可以从 class_copy***list 获取所有的对应的对象;

=================================

成员变量相关的都是ivar_开头的

ivar_getName(ivar)...

=================================

属性相关的都是property_ 开头

property_getName(array)...

=================================

方法相关的都是method_开头

method_getName(method)...

IMP 是一个方法的实现体,

可以调用 imp()来执行该方法;

=================================

协议相关的都是protocol_开头:

protocol_getName()...

获取协议的方法:

Protocol *__unsafe_unretained  *protocols = class_copyProtocolList

或者 tocol = objc_getProtocol("NSCopying");

=================================

关联对象,被关联的当前对象被释放时会自动释放其的关联对象;

关联对象是在运行时添加的类似成员;

************源代码如下********************************/

void *objc_destructInstance(id obj)

{

if (obj) {

    // Read all of the flags at once for performance.

    bool cxx = obj->hasCxxDtor();

    bool assoc = !UseGC && obj->hasAssociatedObjects();

    bool dealloc = !UseGC;

    // This order is important.

    if (cxx) object_cxxDestruct(obj);

    if (assoc) _object_remove_assocations(obj);

    if (dealloc) obj->clearDeallocating();

}

return obj;

}

=================================

调用一个IMP 

使用NSObject提供的methodForSelector:方法可以获得Method的指针,

通过指针调用实现代码。

-(void)setFilled:(Bool)

void (*setter)(id, SEL, BOOL);

int i;

setter = (void (*)(id, SEL, BOOL))[target

 methodForSelector:@selector(setFilled:)];

for ( i = 0 ; i < 1000 ; i++ )

 setter(targetList[i], @selector(setFilled:), YES);

=================================

使用method swizzling需要注意的问题

Swizzling应该总在+load中执行:Objective-C在运行时会自动调用类的两个方法+load和+initialize。+load会在类初始加载时调用,和+initialize比较+load能保证在类的初始化过程中被加载

Swizzling应该总是在dispatch_once中执行:swizzling会改变全局状态,所以在运行时采取一些预防措施,使用dispatch_once就能够确保代码不管有多少线程都只被执行一次。这将成为method swizzling的最佳实践。

Selector,Method和Implementation:这几个之间关系可以这样理解,一个类维护一个运行时可接收的消息分发表,分发表中每个入口是一个Method,其中key是一个特定的名称,及SEL,与其对应的实现是IMP即指向底层C函数的指针。

=================================

http://www.tuicool.com/articles/Av63EfJ

推荐阅读!!

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html#//apple_ref/doc/uid/TP40008048-CH100-SW1

你可能感兴趣的:(Objective-C type encodings runtime)