| 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