{"id":643,"date":"2016-10-12T00:52:29","date_gmt":"2016-10-11T16:52:29","guid":{"rendered":"http:\/\/www.olinone.com\/?p=643"},"modified":"2016-10-12T00:53:09","modified_gmt":"2016-10-11T16:53:09","slug":"protocol%e5%8d%8f%e8%ae%ae%e5%88%86%e5%8f%91%e5%99%a8","status":"publish","type":"post","link":"http:\/\/www.olinone.com\/?p=643","title":{"rendered":"Protocol\u534f\u8bae\u5206\u53d1\u5668"},"content":{"rendered":"<p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\uff1a<a title=\"http:\/\/www.olinone.com\/\" href=\"http:\/\/www.olinone.com\/\">http:\/\/www.olinone.com\/<\/a><\/p>\n<p>Hi\uff0c\u672c\u671f\u8ddf\u5927\u5bb6\u804a\u804a\u534f\u8bae\u5206\u53d1\uff0c\u4f55\u4e3a\u534f\u8bae\u5206\u53d1\uff1f\u534f\u8bae\u5206\u53d1\u53ef\u4ee5\u7b80\u5355\u7406\u89e3\u4e3a\u5c06\u534f\u8bae\u4ee3\u7406\u4ea4\u7ed9\u591a\u4e2a\u5bf9\u8c61\u5b9e\u73b0\uff01<\/p>\n<p>Protocol\u534f\u8bae\u4ee3\u7406\u5728\u5f00\u53d1\u4e2d\u5e94\u7528\u9891\u7e41\uff0c\u5f00\u53d1\u8005\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e2a\u95ee\u9898\u2014\u2014\u4e8b\u4ef6\u7684\u8fde\u7eed\u4f20\u9012\u3002\u6bd4\u5982\uff0c\u4e3a\u4e86\u9694\u79bb\u5c01\u88c5\uff0c\u5f00\u53d1\u8005\u53ef\u80fd\u7ecf\u5e38\u4f1a\u628atableview\u7684delegate\u6216\u8005datesource\u62bd\u79bb\u51fa\u72ec\u7acb\u7684\u5bf9\u8c61\uff0c\u800c\u5176\u5b83\u5bf9\u8c61\uff08\u6bd4\u5982VC\uff09\u9700\u8981\u83b7\u53d6\u67d0\u4e9bdelegate\u4e8b\u4ef6\u65f6\uff0c\u53ea\u80fd\u901a\u8fc7\u4e8b\u4ef6\u7684\u4e8c\u6b21\u4f20\u9012\u3002\u6709\u6ca1\u6709\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\u4e86\uff1f\u534f\u8bae\u5206\u53d1\u5668\u6b63\u597d\u53ef\u4ee5\u6d3e\u4e0a\u7528\u573a<\/p>\n<p>\u8bdd\u4e0d\u591a\u8bf4\uff0c\u5148\u4e0a\u5e72\u8d27\uff1a<a href=\"https:\/\/github.com\/panghaijiao\/HJProtocolDispatcher\">HJProtocolDispatcher<\/a>\u662f\u4e00\u4e2a\u534f\u8bae\u5b9e\u73b0\u5206\u53d1\u5668\uff0c\u901a\u8fc7\u8be5\u5de5\u5177\u80fd\u591f\u8f7b\u6613\u5b9e\u73b0\u5c06\u534f\u8bae\u4e8b\u4ef6\u5206\u53d1\u7ed9\u591a\u4e2a\u5b9e\u73b0\u8005\u3002\u6bd4\u5982\u6700\u5e38\u89c1\u7684tableview\u7684delegate\u534f\u8bae\uff0c\u901a\u8fc7<a href=\"https:\/\/github.com\/panghaijiao\/HJProtocolDispatcher\">HJProtocolDispatcher<\/a>\uff0c\u80fd\u591f\u975e\u5e38\u5bb9\u6613\u7684\u5206\u53d1\u7ed9\u591a\u4e2a\u5bf9\u8c61\uff0c\u5177\u4f53\u53ef\u53c2\u8003<a href=\"https:\/\/github.com\/panghaijiao\/HJProtocolDispatcher\">Demo<\/a><\/p>\n<pre class=\"lang:objc highlight:0 decode:true\">self.tableView.delegate = AOProtocolDispatcher(UITableViewDelegate, self, self.delegateSource);<\/pre>\n<h2>\u539f\u7406\u89e3\u6790<\/h2>\n<p>\u539f\u7406\u5e76\u4e0d\u590d\u6742\uff0c\u00a0\u534f\u8bae\u5206\u53d1\u5668Dispatcher\u5e76\u4e0d\u5b9e\u73b0Protocol\u534f\u8bae\uff0c\u5176\u53ea\u9700\u5c06\u5bf9\u5e94\u7684Protocol\u4e8b\u4ef6\u5206\u53d1\u7ed9\u4e0d\u540c\u7684\u5b9e\u73b0\u8005Implemertor\u3002\u5982\u4f55\u5b9e\u73b0\u5206\u53d1\uff1f<\/p>\n<p>\u719f\u6089\u7c7bClass\u54cd\u5e94\u94fe\u7684\u7ae5\u978b\u90fd\u77e5\u9053\uff0cNSObject\u5bf9\u8c61\u4e3b\u8981\u901a\u8fc7\u4ee5\u4e0b\u51fd\u6570\u54cd\u5e94\u672a\u5b9e\u73b0\u7684Selector\u51fd\u6570\u8c03\u7528<\/p>\n<pre class=\"lang:objc highlight:0 decode:true \">- (id)forwardingTargetForSelector:(SEL)aSelector OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);\r\n- (void)forwardInvocation:(NSInvocation *)anInvocation OBJC_SWIFT_UNAVAILABLE(\"\");\r\n- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector<\/pre>\n<p>\u56e0\u6b64\uff0c\u534f\u8bae\u5206\u53d1\u5668Dispatcher\u53ef\u4ee5\u5728\u8be5\u51fd\u6570\u4e2d\u5c06Protocol\u4e2dSelector\u7684\u8c03\u7528\u4f20\u9012\u7ed9\u5b9e\u73b0\u8005Implemertor\uff0c\u7531\u5b9e\u73b0\u8005Implemertor\u5b9e\u73b0\u5177\u4f53\u7684Selector\u51fd\u6570\u5373\u53ef<\/p>\n<pre class=\"lang:objc highlight:0 decode:true \">- (void)forwardInvocation:(NSInvocation *)anInvocation {\r\n    SEL aSelector = anInvocation.selector;\r\n    if (!ProtocolContainSel(self.prococol, aSelector)) {\r\n        [super forwardInvocation:anInvocation];\r\n        return;\r\n    }\r\n    \r\n    for (ImplemertorContext *implemertorContext in self.implemertors) {\r\n        if ([implemertorContext.implemertor respondsToSelector:aSelector]) {\r\n            [anInvocation invokeWithTarget:implemertorContext.implemertor];\r\n        }\r\n    }\r\n}<\/pre>\n<h2>\u8bbe\u8ba1\u5173\u952e<\/h2>\n<p>\u5982\u4f55\u505a\u5230\u53ea\u5bf9Protocol\u4e2dSelector\u51fd\u6570\u7684\u8c03\u7528\u505a\u5206\u53d1\u662f\u8bbe\u8ba1\u7684\u5173\u952e\uff0c\u7cfb\u7edf\u63d0\u4f9b\u6709\u51fd\u6570<\/p>\n<pre class=\"lang:objc highlight:0 decode:true\">objc_method_description protocol_getMethodDescription(Protocol *p, SEL aSel, BOOL isRequiredMethod, BOOL isInstanceMethod)<\/pre>\n<p>\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5373\u53ef\u5224\u65adSelector\u662f\u5426\u5c5e\u4e8e\u67d0\u4e00Protocol<\/p>\n<pre class=\"lang:objc highlight:0 decode:true \">struct objc_method_description MethodDescriptionForSELInProtocol(Protocol *protocol, SEL sel) {\r\n    struct objc_method_description description = protocol_getMethodDescription(protocol, sel, YES, YES);\r\n    if (description.types) {\r\n        return description;\r\n    }\r\n    description = protocol_getMethodDescription(protocol, sel, NO, YES);\r\n    if (description.types) {\r\n        return description;\r\n    }\r\n    return (struct objc_method_description){NULL, NULL};\r\n}\r\n\r\nBOOL ProtocolContainSel(Protocol *protocol, SEL sel) {\r\n    return MethodDescriptionForSELInProtocol(protocol, sel).types ? YES: NO;\r\n}<\/pre>\n<h2>\u6ce8\u610f\u4e8b\u9879<\/h2>\n<p>\u534f\u8bae\u5206\u53d1\u5668\u4f7f\u7528\u9700\u8981\u4e86\u89e3\u5982\u4f55\u5904\u7406\u5e26\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570 \uff0c\u6bd4\u5982<\/p>\n<pre class=\"lang:objc highlight:0 decode:true \">- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section<\/pre>\n<p>\u6211\u4eec\u77e5\u9053\uff0ciOS\u4e2d\uff0c\u51fd\u6570\u6267\u884c\u8fd4\u56de\u7684\u7ed3\u679c\u5b58\u5728\u4e8e\u5bc4\u5b58\u5668R0\u4e2d\uff0c\u540e\u6267\u884c\u7684\u4f1a\u8986\u76d6\u5148\u6267\u884c\u7684\u7ed3\u679c\u3002\u56e0\u6b64\uff0c\u5f53\u9047\u5230\u6709\u8fd4\u56de\u7ed3\u679c\u7684\u51fd\u6570\u65f6\uff0c\u8fd4\u56de\u7ed3\u679c\u4ee5\u540e\u6267\u884c\u7684\u51fd\u6570\u8fd4\u56de\u7ed3\u679c\u4e3a\u6700\u7ec8\u503c\uff0c\u4ee5Demo\u4e3a\u4f8b<\/p>\n<pre class=\"lang:objc highlight:0 decode:true \">self.tableView.delegate = AOProtocolDispatcher(UITableViewDelegate, self, self.delegateSource);<\/pre>\n<p>TableView\u7684DataSource\u4ee5\u540e\u9762\u7684self.delegateSource\u4e2d\u5b9e\u73b0\u51fd\u6570\u8fd4\u56de\u7684\u7ed3\u679c\u4e3a\u51c6<\/p>\n<h2>\u5907\u6ce8<\/h2>\n<p>\u5f00\u53d1\u5b8c\u672c\u9879\u76ee\u540e\u53d1\u73b0\u7f51\u4e0a\u5df2\u6709\u670b\u53cb\u5b9e\u73b0\u4e86\u534f\u8bae\u5206\u53d1\u5668<a href=\"https:\/\/github.com\/alessandroorru\/AOMultiproxier\">AOMultiproxier<\/a>\uff0c\u56e0\u6b64\uff0c\u6280\u672f\u7248\u6743\u5c5e\u4e8e\u539f\u4f5c\u8005\uff0c\u672c\u6587\u53ea\u505a\u5ba3\u4f20\uff0c\u7279\u6b64\u8bf4\u660e\uff01<\/p>\n<hr \/>\n<p><em>\u5199\u5728\u6587\u540e\uff1a<\/em><\/p>\n<p><em>\u770b\u65f6\u95f4\u5927\u5bb6\u53ef\u80fd\u4e5f\u53d1\u73b0\uff0c\u9879\u76ee\u4ee3\u7801\u63d0\u4ea4\u5df2\u6709\u4e00\u6bb5\u65f6\u95f4\uff0c \u4e3a\u5565\u8fd9\u4e48\u4e45\u624d\u66f4\u65b0\u6b64\u6587\u7ae0\uff1f\u5176\u5b9e\uff0c\u6700\u8fd1\u5b8c\u6210\u4e86\u4e00\u6b21\u957f\u9014\u65c5\u884c\u2014\u2014\u5ddd\u85cf\u884c\uff0c\u7b97\u4f5c\u7ed9\u81ea\u5df1\u7684\u4e00\u6b21\u5fc3\u7075\u6d17\u6da4\u5427\uff01<\/em><\/p>\n<p><em>\u5982\u679c\u4f60\u559c\u6b22\u672c\u6587\u7ae0\uff0c<a href=\"https:\/\/github.com\/panghaijiao\/HJProtocolDispatcher\">Demo<\/a>\u6b22\u8fce\u4f60\u7684\u70b9\u8d5e<\/em><\/p>\n<p><em>\u65b0\u5efa\u4e86\u4e00\u4e2aiOS\u5f00\u53d1QQ\u4ea4\u6d41\u7fa4\uff08\u9996\u9875\u53f3\u4e0a\u89d2\u5165\u7fa4\uff09\uff0c\u6b22\u8fce\u5e7f\u5927iOS\u5f00\u53d1\u670b\u53cb\u4e00\u540c\u4ea4\u6d41\u5b66\u4e60\u3002\u5f53\u7136\uff0c\u4f60\u4e5f\u53ef\u4ee5Follow\u672c\u4eba<a href=\"https:\/\/github.com\/panghaijiao\">GitHub<\/a>\uff0c\u6216\u8005\u5173\u6ce8\u6211\u7684<a href=\"http:\/\/weibo.com\/u\/5474633407\">\u65b0\u6d6a\u5fae\u535a<\/a>\uff0c\u611f\u8c22\u4f60\u7684\u6765\u8bbf\uff0c\u4e0b\u671f\u518d\u89c1\uff01<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904\uff1ahttp:\/\/www.olinone.com\/ Hi\uff0c\u672c\u671f\u8ddf\u5927\u5bb6\u804a\u804a\u534f\u8bae\u5206\u53d1\uff0c\u4f55\u4e3a\u534f\u8bae\u5206\u53d1\uff1f\u534f\u2026 <span class=\"read-more\"><a href=\"http:\/\/www.olinone.com\/?p=643\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"http:\/\/www.olinone.com\/index.php?rest_route=\/wp\/v2\/posts\/643"}],"collection":[{"href":"http:\/\/www.olinone.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.olinone.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.olinone.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.olinone.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=643"}],"version-history":[{"count":26,"href":"http:\/\/www.olinone.com\/index.php?rest_route=\/wp\/v2\/posts\/643\/revisions"}],"predecessor-version":[{"id":672,"href":"http:\/\/www.olinone.com\/index.php?rest_route=\/wp\/v2\/posts\/643\/revisions\/672"}],"wp:attachment":[{"href":"http:\/\/www.olinone.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.olinone.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=643"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.olinone.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}