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)
{

}```
函數(shù)參數(shù)類型: "v@:"

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

oc語(yǔ)言相關(guān)的都是Objc_開頭

objc_getMetaClass(class_getName(cls))...

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

對(duì)象相關(guān)的object_開發(fā)

object_setClass...

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

類相關(guān)的操作都是class_開頭的

class_getName()...

成員變量,屬性,成員函數(shù),都是類相關(guān)的都可以用class_get***獲得對(duì)應(yīng)的指針;

也可以從 class_copy***list 獲取所有的對(duì)應(yīng)的對(duì)象;

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

成員變量相關(guān)的都是ivar_開頭的

ivar_getName(ivar)...

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

屬性相關(guān)的都是property_ 開頭

property_getName(array)...

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

方法相關(guān)的都是method_開頭

method_getName(method)...

IMP 是一個(gè)方法的實(shí)現(xiàn)體,

可以調(diào)用 imp()來(lái)執(zhí)行該方法;

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

協(xié)議相關(guān)的都是protocol_開頭:

protocol_getName()...

獲取協(xié)議的方法:

Protocol *__unsafe_unretained  *protocols = class_copyProtocolList

或者 tocol = objc_getProtocol("NSCopying");

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

關(guān)聯(lián)對(duì)象,被關(guān)聯(lián)的當(dāng)前對(duì)象被釋放時(shí)會(huì)自動(dòng)釋放其的關(guān)聯(lián)對(duì)象;

關(guān)聯(lián)對(duì)象是在運(yùn)行時(shí)添加的類似成員;

************源代碼如下********************************/

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;

}

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

調(diào)用一個(gè)IMP 

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

通過(guò)指針調(diào)用實(shí)現(xiàn)代碼。

-(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應(yīng)該總在+load中執(zhí)行:Objective-C在運(yùn)行時(shí)會(huì)自動(dòng)調(diào)用類的兩個(gè)方法+load和+initialize。+load會(huì)在類初始加載時(shí)調(diào)用,和+initialize比較+load能保證在類的初始化過(guò)程中被加載

Swizzling應(yīng)該總是在dispatch_once中執(zhí)行:swizzling會(huì)改變?nèi)譅顟B(tài),所以在運(yùn)行時(shí)采取一些預(yù)防措施,使用dispatch_once就能夠確保代碼不管有多少線程都只被執(zhí)行一次。這將成為method swizzling的最佳實(shí)踐。

Selector,Method和Implementation:這幾個(gè)之間關(guān)系可以這樣理解,一個(gè)類維護(hù)一個(gè)運(yùn)行時(shí)可接收的消息分發(fā)表,分發(fā)表中每個(gè)入口是一個(gè)Method,其中key是一個(gè)特定的名稱,及SEL,與其對(duì)應(yīng)的實(shí)現(xiàn)是IMP即指向底層C函數(shù)的指針。

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

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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)至元數(shù)據(jù)結(jié)尾創(chuàng)建: 董瀟偉,最新修改于: 十二月 23, 2016 轉(zhuǎn)至元數(shù)據(jù)起始第一章:isa和Class一....
    40c0490e5268閱讀 2,041評(píng)論 0 9
  • 我們常常會(huì)聽說(shuō) Objective-C 是一門動(dòng)態(tài)語(yǔ)言,那么這個(gè)「動(dòng)態(tài)」表現(xiàn)在哪呢?我想最主要的表現(xiàn)就是 Obje...
    Ethan_Struggle閱讀 2,327評(píng)論 0 7
  • 本文詳細(xì)整理了 Cocoa 的 Runtime 系統(tǒng)的知識(shí),它使得 Objective-C 如虎添翼,具備了靈活的...
    lylaut閱讀 866評(píng)論 0 4
  • 轉(zhuǎn)載:http://yulingtianxia.com/blog/2014/11/05/objective-c-r...
    F麥子閱讀 828評(píng)論 0 2
  • 1.1 什么是自動(dòng)引用計(jì)數(shù) 概念:在 LLVM 編譯器中設(shè)置 ARC(Automaitc Reference Co...
    __silhouette閱讀 5,469評(píng)論 1 17

友情鏈接更多精彩內(nèi)容