1.为ä»ä¹ç¨åºä¼å¨objc
2.selfåsuperçåºå«
为ä»ä¹ç¨åºä¼å¨objc
ç¨åºå´©æºå¨objc_msgSend()ï¼ææå¯è½çåå æ¯åä¸ä¸ªå·²ç»éæ¾ç对象åéæ¶æ¯ã
å ¶ä»çåå å¦ä¸ï¼
1.ä¹è®¸ä½ çæéæ¯å®å ¨æ£ç¡®çï¼ä½æ¯å«ç缺è³èå°è ¿ç对象çå 容å¯è½æ¯æº¢åºå¨éè¿çä¸ä¸ªåé çç¼å²åºã
2. æè 使ç¨éå¤çæéæåç°å¨ä½ ç对象æå ç¨çå åã
3.æ个å«objc_msgSendï¼ï¼å´©æºæ¯å 为å åè¢«ç ´åï¼ä½é常çé®é¢æ¯å¨æ¥æ¶å¯¹è±¡æ¬èº«ã
解å³é®é¢çæ¹æ³
1. æ¾å°æ¥æ¶æ¶æ¯ç对象å转æ¢å¨ï¼selector ï¼æ³¨åçææ对象ã
objc_msgSend()åå¨äºreceiveråthe selectorå¨cpuçå¯åå¨ä¸ãä¸åçæ¶æçcpuå¯åå¨çå称ä¸å
objc_msgSend
objc_msgSend_fpret objc_msgSend_stret
receiver SEL receiver SEL
i eax* ecx eax* ecx
x_ rdi rsi rsi rdx
ppc r3 r4 r4 r5
ppc r3 r4 r4 r5
arm r0 r1 r1 r2
arm x0 x1 â â
å¨è°è¯å¨æ§å¶å°ä¸ï¼å½ç¨åºåæ¢æ¶ï¼æå°æ æå°åï¼æ¨å¯ä»¥æå°ä¸é¢ç表ä¸ä½¿ç¨çå¯åå¨å称çæ¥æ¶è çå°åã
以ä¸æ¯å¨Xcode 5.1è¿æ¥ipadçç»æ
(lldb) re r -f b r0
r0 = 0b
转æ¢æè¿å¶
(lldb) re r -f b r0 --format x
r0 = 0xbcc
对åºçgdbçæ令为
(gdb) p/x $eax
$1 = 0x1
selfåsuperçåºå«
è¦ç¹ï¼
selfè°ç¨èªå·±æ¹æ³ï¼superè°ç¨ç¶ç±»æ¹æ³
selfæ¯ç±»ï¼superæ¯é¢ç¼è¯æ令
ãself classãåãsuper classãè¾åºæ¯ä¸æ ·ç
selfåsuperåºå±å®ç°åçï¼
å½ä½¿ç¨ self è°ç¨æ¹æ³æ¶ï¼ä¼ä»å½åç±»çæ¹æ³å表ä¸å¼å§æ¾ï¼å¦æ没æï¼å°±ä»ç¶ç±»ä¸åæ¾ï¼èå½ä½¿ç¨ super æ¶ï¼åä»ç¶ç±»çæ¹æ³å表ä¸å¼å§æ¾ï¼ç¶åè°ç¨ç¶ç±»çè¿ä¸ªæ¹æ³ã
å½ä½¿ç¨ self è°ç¨æ¶ï¼ä¼ä½¿ç¨ objc_msgSend å½æ°ï¼ id objc_msgSend(id theReceiver,semop函数源码 SEL theSelector, ...)ã第 ä¸ä¸ªåæ°æ¯æ¶æ¯æ¥æ¶è ï¼ç¬¬äºä¸ªåæ°æ¯è°ç¨çå ·ä½ç±»æ¹æ³ç selectorï¼åé¢æ¯ selector æ¹æ³çå¯ååæ°ã以 [self setName:] 为ä¾ï¼ç¼è¯å¨ä¼æ¿æ¢æè°ç¨ objc_msgSend çå½æ°è°ç¨ï¼å ¶ä¸ theReceiver æ¯ selfï¼theSelector æ¯ @selector(setName:)ï¼è¿ä¸ª selector æ¯ä»å½å self ç class çæ¹æ³å表å¼å§æ¾ç setNameï¼å½æ¾å°åæ对åºç selector ä¼ éè¿å»ã
å½ä½¿ç¨ super è°ç¨æ¶ï¼ä¼ä½¿ç¨ objc_msgSendSuper å½æ°ï¼id objc_msgSendSuper(struct objc_super *super, SEL op, ...)第ä¸ä¸ªåæ°æ¯ä¸ªobjc_superçç»æä½ï¼ç¬¬äºä¸ªåæ°è¿æ¯ç±»ä¼¼ä¸é¢çç±»æ¹æ³çselectorï¼
struct objc_super {
id receiver;
Class superClass;
};
å½ç¼è¯å¨éå° [super setName:] æ¶ï¼å¼å§åè¿å 个äºï¼
1ï¼æ 建 objc_super çç»æä½ï¼æ¤æ¶è¿ä¸ªç»æä½ç第ä¸ä¸ªæååé receiver å°±æ¯ åç±»ï¼å self ç¸åãè第äºä¸ªæååé superClass å°±æ¯æç¶ç±»
è°ç¨ objc_msgSendSuper çæ¹æ³ï¼å°è¿ä¸ªç»æä½å setName ç sel ä¼ éè¿å»ã
2ï¼å½æ°éé¢å¨åçäºæ 类似è¿æ ·ï¼ä» objc_super ç»æä½æåç superClass çæ¹æ³å表å¼å§æ¾ setName ç selectorï¼æ¾å°åå以 objc_super->receiver å»è°ç¨è¿ä¸ª selector