{"id":1395,"date":"2018-11-20T17:30:37","date_gmt":"2018-11-20T09:30:37","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=1395"},"modified":"2020-12-01T12:45:23","modified_gmt":"2020-12-01T04:45:23","slug":"objective-c%e6%b6%88%e6%81%af%e5%8f%91%e9%80%81%e6%9c%ba%e5%88%b6%e5%8e%9f%e7%90%86objc_msgsend","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/project\/2018\/11\/20\/1730371395.html","title":{"rendered":"Objective-C\u6d88\u606f\u53d1\u9001\u673a\u5236\u539f\u7406objc_msgSend( )"},"content":{"rendered":"<p><strong>0\u3001\u65b9\u6cd5\u8c03\u7528\u539f\u7406<\/strong><br \/>\n0.1\u3001\u4ee5[reciver method]\u4e3a\u4f8b\uff0c\u5b9e\u9645\u4e0a\u662fobjc_msgSend(reciver, @selector(method), &#8230;argments);<br \/>\n0.2\u3001OC\u65b9\u6cd5\u8c03\u7528\u8f6c\u6362\u4e3aobjc_msgSend\u51fd\u6570\u8c03\u7528\uff0c\u65b9\u6cd5\u8c03\u7528\u8005\u3001\u65b9\u6cd5\u540d\u4e3a\u8be5\u51fd\u6570\u7684\u524d2\u4e2a\u53c2\u6570\uff1b<br \/>\n0.3\u3001OC\u5bf9\u8c61\u672c\u8d28\u4e3a\u7ed3\u6784\u4f53\uff0c\u53c2\u8003\uff1a<a href=\"http:\/\/www.yusian.com\/blog\/article\/2018\/11\/15\/113919.html\" rel=\"noopener noreferrer\" target=\"_blank\">\u5256\u6790Objective-C\u5bf9\u8c61\u672c\u8d28\u7ed3\u6784MJClassInfo<\/a><\/p>\n<p><strong>1\u3001\u6d88\u606f\u53d1\u9001\uff08\u67e5\u627e\u65b9\u6cd5\uff09<\/strong><br \/>\n1.1\u3001\u5982\u679c\u65b9\u6cd5\u8c03\u7528\u8005\u4e3anil\uff0c\u76f4\u63a5\u8fd4\u56de(\u4e0d\u62a5\u9519);<br \/>\n2.2\u3001\u65b9\u6cd5\u8c03\u7528\u8005\u901a\u8fc7isa\u6307\u9488\u67e5\u627e\u65b9\u6cd5\u63a5\u6536\u8005\uff0c\u65b9\u6cd5\u63a5\u6536\u8005\u67e5\u627e\u5bf9\u8c61\u4e2d\u7684cache\uff0c\u5982\u679c\u6ca1\u6709\u5219\u67e5\u627e\u5bf9\u8c61\u4e2d\u65b9\u6cd5\u5217\u8868class_rw_t\uff1b<br \/>\n2.3\u3001\u5982\u679c\u627e\u5230\u5219\u8c03\u7528\u5e76\u7f13\u5b58\u5728cache\u4e2d\uff0c\u5426\u5219\u901a\u8fc7superclass\u5728\u7236\u7c7b\u4e2d\u7684cache\uff0cclass_rw_t\u4e2d\u67e5\u627e\uff1b<br \/>\n2.4\u3001\u5982\u679c\u627e\u5230\u5219\u8c03\u7528\u5e76\u7f13\u5b58\u5728cache\u4e2d\uff0c\u5426\u5219\u518d\u901a\u8fc7superclass\u7684superclass\u67e5\u627e\uff0c\u76f4\u5230superclass\u4e3a\u7a7a\uff1b<br \/>\n2.5\u3001\u5982\u679c\u5728\u6574\u4e2a\u7ee7\u627f\u4f53\u7cfb\u4e2d\u90fd\u6ca1\u6709\u627e\u5230\u76ee\u6807\u65b9\u6cd5\uff0c\u8c03\u7528\u65b9\u6cd5\u63a5\u6536\u8005\u7684\u52a8\u6001\u89e3\u6790\u65b9\u6cd5(\u5982\u679c\u89e3\u6790\u8fc7\u5219\u8fdb\u5165\u65b9\u6cd5\u8f6c\u53d1)\uff1b<\/p>\n<p><strong>2\u3001\u52a8\u6001\u89e3\u6790<\/strong><!--more--><br \/>\n2.1\u3001\u4e24\u4e2a\u76f8\u5173\u65b9\u6cd5\uff1aresoloveInstanceMethod:\u3001resoloveClassMethod:\u524d\u8005\u4e3a\u5b9e\u4f8b\u5bf9\u8c61\u52a8\u6001\u89e3\u6790\uff0c\u540e\u8005\u4e3a\u7c7b\\\u5143\u7c7b\u5bf9\u8c61\u52a8\u6001\u89e3\u6790\uff1b<\/p>\n<p>2.2\u3001\u8fdb\u5165\u52a8\u6001\u89e3\u6790\u65b9\u6cd5\uff0c\u9700\u8981\u5b8c\u6210\u7684\u5de5\u4f5c\u662f\u7ed9\u65b9\u6cd5\u63a5\u6536\u8005\u6dfb\u52a0\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5982\u901a\u8fc7class_addMetho()\u51fd\u6570\u5b9e\u73b0\uff1b<\/p>\n<pre lang=\"c\" line=\"1\">class_addMethod(Class cls, SEL name, IMP imp, const char *types)<\/pre>\n<p>2.2.1\u3001\u7b2c\u4e00\u53c2\u6570\u4e3a\u65b9\u6cd5\u63a5\u6536\u8005\uff1a\u5b9e\u4f8b\u65b9\u6cd5\u8c03\u7528\u5219cls\u4e3a\u7c7b\u5bf9\u8c61\uff0c\u7c7b\u65b9\u6cd5\u8c03\u7528\u5219\u4e3a\u5143\u7c7b\u5bf9\u8c61\uff1b<br \/>\n2.2.2\u3001\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a\u65b9\u6cd5\u540d\uff0c\u53ef\u76f4\u63a5\u4f7f\u7528\u4f20\u8fdb\u6765\u7684\u5f62\u53c2\uff1b<br \/>\n2.2.3\u3001\u7b2c\u4e09\u4e2a\u53c2\u6570\u4e0e\u7b2c\u56db\u4e2a\u53c2\u6570\u4e0e\u65b9\u6cd5\u5bf9\u8c61Method\u76f8\u5173\uff0c\u53ef\u901a\u8fc7method_getImplementation()\u3001method_getTypeEncoding()\u83b7\u53d6\uff0c\u800cMethod\u5bf9\u8c61\u53ef\u901a\u8fc7class_getClassMethod(Class cls, SEL sel)\u83b7\u53d6\uff1b<\/p>\n<p>2.3\u3001\u6dfb\u52a0\u65b9\u6cd5\u5b9e\u73b0\u540e\uff0c\u539f\u6765\u8c03\u7528sel\u65b9\u6cd5\u52a8\u4f5c\u53d8\u6210\u8c03\u7528method\u65b9\u6cd5\uff0c\u89e3\u6790\u540e\u7684\u65b9\u6cd5\u4e0d\u5173\u5fc3\u65b9\u6cd5\u6027\u8d28\uff0c\u5373\u5bf9\u8c61\u65b9\u6cd5\u6216\u7c7b\u65b9\u6cd5\u90fd\u53ef\u4ee5\uff1b<br \/>\n2.4\u3001\u89e3\u6790\u5b8c\u6210\u540e\uff0c\u518d\u6b21\u6267\u884c\u65b9\u6cd5\u8c03\u7528\u5404\u6b65\u9aa4\uff0c\u5982\u679c\u67e5\u627e\u5230\u4e86\u52a8\u6001\u65b9\u6cd5\u5219\u6267\u884c\uff0c\u5982\u679c\u8fd8\u662f\u6ca1\u6709\u627e\u5230\uff0c\u4e0d\u518d\u8fdb\u884c\u52a8\u6001\u89e3\u6790\uff0c\u8fdb\u5165\u65b9\u6cd5\u8f6c\u53d1\u9636\u6bb5\uff1b<\/p>\n<p><strong>3\u3001\u65b9\u6cd5\u8f6c\u53d1<\/strong><br \/>\n3.1\u3001\u76f8\u5173\u65b9\u6cd5<\/p>\n<pre lang=\"objc\" line=\"1\">\r\n- (id)forwardingTargetForSelector:(SEL)aSelector;\r\n+ (id)forwardingTargetForSelector:(SEL)aSelector;\r\n\r\n- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector;\r\n+ (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector;\r\n- (void)forwardInvocation:(NSInvocation *)anInvocation;\r\n+ (void)forwardInvocation:(NSInvocation *)anInvocation;<\/pre>\n<p>3.1\u3001\u6d88\u606f\u8f6c\u53d1\u9996\u5148\u8c03\u7528forwardingTargetForSelector:\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u8981\u6c42\u8fd4\u56de\u4e00\u4e2a\u5b9e\u73b0\u4e86\u539f\u65b9\u6cd5\u7684\u5bf9\u8c61\uff1b<br \/>\n3.1.1\u3001\u5982\u679c\u8fd4\u56de\u5bf9\u8c61\u4e0d\u4e3a\u7a7a\uff0c\u5219\u8c03\u7528\u5bf9\u8c61\u7684aSelector\u65b9\u6cd5\uff0c\u8fd9\u4e5f\u662f\u4e00\u4e2a\u65b9\u6cd5\u8c03\u7528\uff0c\u540c\u6837\u7b26\u5408\u5f53\u524d\u6240\u6709\u7684\u673a\u5236\uff0c\u5373\u4ece\u6d88\u606f\u53d1\u9001\u5f00\u59cb&#8212;>\u52a8\u6001\u89e3\u6790&#8212;->\u65b9\u6cd5\u8f6c\u53d1\u8fd9\u4e00\u7cfb\u5217\u52a8\u4f5c\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u662f\u4e00\u6b21\u9012\u5f52\uff1b<br \/>\n3.1.2\u3001\u5982\u679c\u8be5\u65b9\u6cd5\u6ca1\u6709\u5b9e\u73b0\uff0c\u6216\u8fd4\u56de\u4e3a\u7a7a\uff0c\u5219\u8c03\u7528\u65b9\u6cd5\u7b7e\u540d<\/p>\n<p>3.2\u3001\u65b9\u6cd5\u7b7e\u540dmethodSignatureForSelector:\u8be5\u65b9\u6cd5\u53ea\u9700\u8981\u8fd4\u56de\u4e00\u4e2a\u6709\u6548\u7b7e\u540d\u5373\u53ef\uff0c\u63a5\u4e0b\u6765\u4f1a\u8c03\u7528forwardInvocation:<br \/>\n3.2.1\u3001\u5982\u679c\u8fd4\u56de\u4e3a\u7a7a\u6216\u4e0d\u662f\u6709\u6548\u7684NSMethodSignature\u5bf9\u8c61\uff0c\u5219\u8c03\u7528doesNotRecognizeSelector\u65b9\u6cd5\u629b\u51fa\u5f02\u5e38\uff1b<br \/>\n3.2.2\u3001\u5982\u679c\u8fd4\u56de\u4e00\u4e2a\u6709\u6548\u7684NSMethodSignature\uff0cforwardInvocation:\u6ca1\u6709\u5b9e\u73b0\u540c\u6837\u8c03\u7528doesNotRecognizeSelector\u65b9\u6cd5\u629b\u51fa\u5f02\u5e38\uff1b<\/p>\n<p>3.3\u3001forwardInvocation:\u4f1a\u6839\u636e\u65b9\u6cd5\u7b7e\u540d\u7ed9\u51faNSInvocation\u5bf9\u8c61\uff0c\u53ea\u8981\u5b9e\u73b0\u4e86\u8be5\u65b9\u6cd5\u5373\u4f1a\u6267\u884c\u8be5\u65b9\u6cd5\u5185\u7684\u4ee3\u7801\u4e3a\u65b9\u6cd5\u6700\u7ec8\u5f52\u5bbf\uff1b<\/p>\n<p>4\u3001\u5982\u679c\u4ee5\u4e0a\u6240\u6709\u64cd\u4f5c\u4ec0\u4e48\u90fd\u6ca1\u505a\uff0c\u5219\u76f4\u63a5\u8c03\u7528doesNotRecognizeSelector\u65b9\u6cd5\u629b\u51fa\u5f02\u5e38\uff1b<\/p>\n<p>5\u3001\u603b\u7ed3\u8fc7\u7a0b\u4e2d\u7684\u51e0\u4e2a\u65b9\u6cd5<\/p>\n<pre lang=\"objc\" line=\"1\">\r\n\/\/ \u52a8\u6001\u89e3\u6790\r\n+ (BOOL)resolveInstanceMethod:(SEL)sel;\r\n+ (BOOL)resolveClassMethod:(SEL)sel;\r\n\/\/ \u65b9\u6cd5\u8f6c\u53d1\r\n- (id)forwardingSignatureMethodForSelector:(SEL)aSelector;\r\n+ (id)forwardingSignatureMethodForSelector:(SEL)aSelector;\r\n\/\/ \u65b9\u6cd5\u7b7e\u540d\r\n- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector;\r\n+ (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector;\r\n- (void)forwardInvocation:(NSInvocation *)anInvocation;\r\n+ (void)forwardInvocation:(NSInvocation *)anInvocation;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>0\u3001\u65b9\u6cd5\u8c03\u7528\u539f\u7406 0.1\u3001\u4ee5[reciver method]\u4e3a\u4f8b\uff0c\u5b9e\u9645\u4e0a\u662fobjc_msgSend(reciver, @selector(method), &#8230;argments); 0.2\u3001OC\u65b9\u6cd5\u8c03\u7528\u8f6c\u6362\u4e3aobjc_msgSend\u51fd\u6570\u8c03\u7528\uff0c\u65b9\u6cd5\u8c03\u7528\u8005\u3001\u65b9\u6cd5\u540d\u4e3a\u8be5\u51fd\u6570\u7684\u524d2\u4e2a\u53c2\u6570\uff1b 0.3\u3001OC\u5bf9\u8c61\u672c\u8d28\u4e3a\u7ed3\u6784\u4f53\uff0c\u53c2\u8003\uff1a\u5256\u6790Objective-C\u5bf9\u8c61\u672c\u8d28\u7ed3\u6784MJClassInfo 1\u3001\u6d88\u606f\u53d1\u9001\uff08\u67e5\u627e\u65b9\u6cd5\uff09 1.1\u3001\u5982\u679c\u65b9\u6cd5\u8c03\u7528\u8005\u4e3anil\uff0c\u76f4\u63a5\u8fd4\u56de(\u4e0d\u62a5\u9519); 2.2\u3001\u65b9\u6cd5\u8c03\u7528\u8005\u901a\u8fc7isa\u6307\u9488\u67e5\u627e\u65b9\u6cd5\u63a5\u6536\u8005\uff0c\u65b9\u6cd5\u63a5\u6536\u8005\u67e5\u627e\u5bf9\u8c61\u4e2d\u7684cache\uff0c\u5982\u679c\u6ca1\u6709\u5219\u67e5\u627e\u5bf9\u8c61\u4e2d\u65b9\u6cd5\u5217\u8868class_rw_t\uff1b 2.3\u3001\u5982\u679c\u627e\u5230\u5219\u8c03\u7528\u5e76\u7f13\u5b58\u5728cache\u4e2d\uff0c\u5426\u5219\u901a\u8fc7superclass\u5728\u7236\u7c7b\u4e2d\u7684cache\uff0cclass_rw_t\u4e2d\u67e5\u627e\uff1b 2.4\u3001\u5982\u679c\u627e\u5230\u5219\u8c03\u7528\u5e76\u7f13\u5b58\u5728cache\u4e2d\uff0c\u5426\u5219\u518d\u901a\u8fc7superclass\u7684superclass\u67e5\u627e\uff0c\u76f4\u5230superclass\u4e3a\u7a7a\uff1b 2.5\u3001\u5982\u679c\u5728\u6574\u4e2a\u7ee7\u627f\u4f53\u7cfb\u4e2d\u90fd\u6ca1\u6709\u627e\u5230\u76ee\u6807\u65b9\u6cd5\uff0c\u8c03\u7528\u65b9\u6cd5\u63a5\u6536\u8005\u7684\u52a8\u6001\u89e3\u6790\u65b9\u6cd5(\u5982\u679c\u89e3\u6790\u8fc7\u5219\u8fdb\u5165\u65b9\u6cd5\u8f6c\u53d1)\uff1b 2\u3001\u52a8\u6001\u89e3\u6790<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[88,11],"tags":[187,179,184,180,186,183,185],"class_list":["post-1395","post","type-post","status-publish","format-standard","hentry","category-cpp","category-project","tag-objc_msgsend","tag-runtime","tag-184","tag-180","tag-186","tag-183","tag-185"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=1395"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1395\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=1395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=1395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=1395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}