{"id":610,"date":"2013-07-25T23:14:40","date_gmt":"2013-07-25T15:14:40","guid":{"rendered":"http:\/\/xujiwei.com\/blog\/?p=610"},"modified":"2016-12-27T01:30:50","modified_gmt":"2016-12-26T17:30:50","slug":"generics-in-objc-and-code-styler-for-xcode","status":"publish","type":"post","link":"https:\/\/xujiwei.com\/blog\/2013\/07\/generics-in-objc-and-code-styler-for-xcode\/","title":{"rendered":"[\u7b14\u8bb0] Objective-C \u4e2d\u6a21\u62df\u6cdb\u578b\u53ca Xcode \u683c\u5f0f\u5316\u4ee3\u7801\u63d2\u4ef6"},"content":{"rendered":"<p>\u8bb0\u5f55\u4e00\u4e9b iOS &amp; OS X \u5f00\u53d1\u8fc7\u7a0b\u4e2d\u5b66\u5230\u6216\u6709\u8da3\u7684\u4e1c\u897f\u3002<\/p>\n<h2>Objective-C \u4e2d\u6a21\u62df\u6cdb\u578b<\/h2>\n<p>\u4eca\u5929\u770b\u5230\u7684\u4e00\u4e2a\u8f85\u52a9\u4ee3\u7801\uff0c\u53ef\u4ee5\u5728 Objective-C \u4ee3\u7801\u4e2d\u6dfb\u52a0\u6cdb\u578b\u7684\u652f\u6301\uff1a<https: \/\/github.com\/tomersh\/Objective-C-Generics><\/p>\n<blockquote><p>\n  \u6cdb\u578b\u662f\u7a0b\u5e8f\u8bbe\u8ba1\u8bed\u8a00\u7684\u4e00\u79cd\u7279\u6027\u3002\u5141\u8bb8\u7a0b\u5e8f\u5458\u5728\u5f3a\u7c7b\u578b\u7a0b\u5e8f\u8bbe\u8ba1\u8bed\u8a00\u4e2d\u7f16\u5199\u4ee3\u7801\u65f6\u5b9a\u4e49\u4e00\u4e9b\u53ef\u53d8\u90e8\u4efd\uff0c\u90a3\u4e9b\u90e8\u4efd\u5728\u4f7f\u7528\u524d\u5fc5\u9808\u4f5c\u51fa\u6307\u660e\u3002\u5404\u79cd\u7a0b\u5e8f\u8bbe\u8ba1\u8bed\u8a00\u548c\u5176\u7f16\u8bd1\u5668\u3001\u8fd0\u884c\u73af\u5883\u5bf9\u6cdb\u578b\u7684\u652f\u6301\u5747\u4e0d\u4e00\u6837\u3002<em>(via <http: \/\/zh.wikipedia.org\/wiki\/%E6%B3%9B%E5%9E%8B>)<\/http:><\/em>\n<\/p><\/blockquote>\n<p>\u6cdb\u578b\u4e00\u822c\u51fa\u73b0\u5728 .NET\u3001Java \u6216 C++ \u7684\u4ee3\u7801\u91cc\u9762\u3002<\/p>\n<p>\u5728 Objective-C \u91cc\u9762\uff0c\u50cf\u6570\u7ec4\u4e4b\u7c7b\u7684\u5bb9\u5668\uff0c\u5728\u53d6\u5355\u4e2a\u5143\u7d20\u65f6\uff0c\u8fd4\u56de\u7c7b\u578b\u90fd\u662f id\uff0c\u8fd9\u4e2a\u65f6\u5019\u5982\u679c\u8981\u4f7f\u7528\u8fd4\u56de\u5bf9\u8c61\u7684\u4e00\u4e9b\u5c5e\u6027\uff0c\u5c31\u9700\u8981\u5148\u8f6c\u6362\u4e00\u4e0b\u7c7b\u578b\uff0c\u6216\u8005\u4f7f\u7528 getter \u65b9\u6cd5\u6765\u83b7\u53d6\u3002<\/p>\n<p>\u5982\u679c\u7528\u4e86\u6cdb\u578b\u4e4b\u540e\uff0c\u5c31\u5f88\u65b9\u4fbf\u4e86\uff0c\u53ef\u4ee5\u76f4\u63a5\u4ee5 <code>arr.lastObject.prop<\/code> \u7684\u65b9\u5f0f\u6765\u8bbf\u95ee\u3002<\/p>\n<p>\u4f7f\u7528\u4e86\u4e0a\u9762\u8fd9\u4e2a\u4ee3\u7801\u4f7f Objective-C \u652f\u6301\u6cdb\u578b\u540e\uff0c\u5c31\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u5199\u4ee3\u7801\u4e86\uff1a<\/p>\n<p><img src=\"https:\/\/github-camo.global.ssl.fastly.net\/86daa0ef5a26b038405a4ee4b87cee6307b35b51\/687474703a2f2f7331372e706f7374696d672e6f72672f3732797861337637332f73706c6173682e706e67\" alt=\"generics example\" \/><\/p>\n<p>\u8fd9\u4e2a\u8f85\u52a9\u4ee3\u7801\u662f\u4e00\u4e2a\u5927\u5b8f\uff0c\u7ed9 Objective-C \u6dfb\u52a0\u6cdb\u578b\u652f\u6301\u662f\u4ee5\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\u6765\u505a\u5230\u7684\uff0c\u4f8b\u5982\u7ed9 MyClass \u6dfb\u52a0\u6cdb\u578b\u652f\u6301\u6765\u8bf4\u660e\uff1a<\/p>\n<ol>\n<li>\u5b9a\u4e49\u4e00\u4e2a MyClass \u7684 Protocol<\/li>\n<li>\u7ed9\u5404\u4e2a\u5bb9\u5668\u7c7b\uff08\u4f8b\u5982 NSArray\u3001NSMutableArray\u3001NSSet\uff09\u6dfb\u52a0 Category\uff0c\u91cd\u65b0\u5b9a\u4e49\u5404\u4e2a\u5bb9\u5668\u64cd\u4f5c\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u548c\u53c2\u6570\u7c7b\u578b\n<ol>\n<li>\u83b7\u53d6\u5355\u4e2a\u5143\u7d20\u7684\u65b9\u6cd5\u8fd4\u56de\u503c\u7c7b\u578b\u4fee\u6539\u4e3a <code>MyClass *<\/code><\/li>\n<li>\u8fd4\u56de\u5bb9\u5668\u7684\u65b9\u6cd5\u8fd4\u56de\u503c\u7c7b\u578b\u4fee\u6539\u4e3a <code>NSArray&lt;myclass&gt; *<\/code><\/li>\n<li>\u53c2\u6570\u7c7b\u578b\u540c\u6837\u5904\u7406<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p>\u8fd9\u6837\u5728\u4f7f\u7528 <code>NSMutableArray&lt;\/myclass&gt;&lt;myclass&gt; *arr<\/code> \u8fd9\u6837\u4e00\u4e2a\u53d8\u91cf\u65f6\uff0c\u5982\u679c\u6dfb\u52a0\u7684\u5143\u7d20\u4e0d\u662f <code>MyClass *<\/code> \u7c7b\u578b\uff0c\u5c31\u4f1a\u53d1\u51fa\u8b66\u544a\u4e86\u3002<\/p>\n<p>\u4e0d\u8fc7\u56e0\u4e3a Objective-C \u5728\u7f16\u8bd1\u65f6\u5e76\u4e0d\u4f1a\u5f3a\u5236\u8981\u6c42\u53d8\u91cf\u7c7b\u578b\u4e00\u81f4\uff0c\u6240\u4ee5\u5982\u679c\u4f20\u9012\u4e00\u4e2a\u975e <code>MyClass *<\/code>\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u4e5f\u662f\u53ef\u4ee5\u7f16\u8bd1\u901a\u8fc7\u7684\u3002<\/p>\n<p>\u5f53\u7136\u8fd9\u4e2a\u5e93\u53ea\u80fd\u8bf4\u662f\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u65b9\u4fbf\u7f16\u5199\u4ee3\u7801\u800c\u5df2\uff0c\u81f3\u4e8e\u7528\u4e0d\u7528\u8fd8\u662f\u89c1\u4ec1\u89c1\u667a\u4e86\uff0c\u6bcf\u6b21\u8981\u5199 <code>NSArray&lt;\/myclass&gt;&lt;myclass *&gt;<\/code> \u4e5f\u4e0d\u89c1\u5f97\u66f4\u65b9\u4fbf\u3002<\/p>\n<h2>Xcode \u4ee3\u7801\u683c\u5f0f\u5316\u63d2\u4ef6 BBUncrustifyPlugin<\/h2>\n<p>\u63d2\u4ef6\u5730\u5740\uff1a<\/https:><https: \/\/github.com\/benoitsan\/BBUncrustifyPlugin-Xcode><\/p>\n<p>\u8fd9\u4e2a\u63d2\u4ef6\u7b97\u662f\u5bf9 <a href=\"http:\/\/uncrustify.sourceforge.net\/\">uncrustify<\/a> \u7684\u4e00\u4e2a\u5feb\u6377\u5f15\u7528\u4e86\u3002<\/p>\n<p>\u53ef\u80fd\u4f1a\u6bd4 Xcode \u81ea\u5e26\u7684 Re-indent \u529f\u80fd\u5f3a\u5927\u4e00\u70b9\u70b9 :)<\/p>\n<p>\u53e6\u5916\uff0c\u8fd8\u6709\u4e00\u4e2a\u56fe\u5f62\u5316\u7684\u53c2\u6570\u914d\u7f6e\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u914d\u7f6e uncrustify \u90a3\u597d\u51e0\u767e\u9879\u76ee\u7684\u53c2\u6570\uff1a<a href=\"https:\/\/github.com\/ryanmaxwell\/UncrustifyX\">UncrustifyX<\/a><\/p>\n<h2>\u53c2\u8003\u8d44\u6599<\/h2>\n<ol>\n<li>http:\/\/iosdevelopertips.com\/objective-c\/generics-in-objective-c.html<\/li>\n<li>https:\/\/github.com\/tomersh\/Objective-C-Generics<\/li>\n<li>https:\/\/github.com\/benoitsan\/BBUncrustifyPlugin-Xcode<\/li>\n<li>http:\/\/zh.wikipedia.org\/wiki\/%E6%B3%9B%E5%9E%8B<\/li>\n<li>http:\/\/uncrustify.sourceforge.net<\/li>\n<\/ol>\n<p>---EOF---<\/https:><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8bb0\u5f55\u4e00\u4e9b iOS &amp; OS X \u5f00\u53d1\u8fc7\u7a0b\u4e2d\u5b66\u5230\u6216\u6709\u8da3\u7684\u4e1c\u897f\u3002 Objec &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/xujiwei.com\/blog\/2013\/07\/generics-in-objc-and-code-styler-for-xcode\/\">\u7ee7\u7eed\u9605\u8bfb &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[168],"tags":[240,211,241,177],"_links":{"self":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts\/610"}],"collection":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/comments?post=610"}],"version-history":[{"count":0,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts\/610\/revisions"}],"wp:attachment":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/media?parent=610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/categories?post=610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/tags?post=610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}