{"id":568,"date":"2023-02-25T19:12:22","date_gmt":"2023-02-25T11:12:22","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=568"},"modified":"2023-04-29T20:34:02","modified_gmt":"2023-04-29T12:34:02","slug":"swift-class-extension","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/swift-class-extension\/","title":{"rendered":"Swift &#8211; \u7c7b\u6269\u5c55\uff08extension\uff09"},"content":{"rendered":"<blockquote>\n<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4<\/p>\n<\/blockquote>\n<p>Swift\u8bed\u8a00\u7684\u7c7b\u6269\u5c55\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7c7b\u6269\u5c55\u5b8c\u6210\u5982\u4e0b\u4e8b\u60c5\uff1a<\/p>\n<p><!-- more --><\/p>\n<ul>\n<li>\u7ed9\u5df2\u6709\u7684\u7c7b\u6dfb\u52a0\u8ba1\u7b97\u5c5e\u6027\u548c\u8ba1\u7b97\u9759\u6001\u5c5e\u6027<\/li>\n<li>\u5b9a\u4e49\u65b0\u7684\u5b9e\u4f8b\u65b9\u6cd5\u548c\u7c7b\u65b9\u6cd5<\/li>\n<li>\u63d0\u4f9b\u65b0\u7684\u6784\u9020\u5668<\/li>\n<li>\u5b9a\u4e49\u4e0b\u6807\u811a\u672c<\/li>\n<li>\u4f7f\u4e00\u4e2a\u5df2\u6709\u7684\u7c7b\u578b\u7b26\u5408\u67d0\u4e2a\u534f\u8bae<\/li>\n<\/ul>\n<blockquote>\n<p>\u6ce8\uff1a\u6269\u5c55\u53ea\u80fd\u6dfb\u52a0\u65b0\u7684\u8ba1\u7b97\u578b\u5c5e\u6027\uff0c\u4e0d\u80fd\u6dfb\u52a0\u5b58\u50a8\u578b\u5c5e\u6027\uff0c\u4e5f\u4e0d\u80fd\u6dfb\u52a0\u65b0\u7684\u5c5e\u6027\u76d1\u89c6\u5668\u3002<br \/>\n\u6ce8\uff1a\u5728 Swift 4 \u4e2d\uff0cextension \u91cc\u53ef\u4ee5\u8bbf\u95ee private \u7684\u5c5e\u6027\u3002\uff08\u8fc7\u53bb Swift 3 \u4e2d\u662f\u4e0d\u884c\u7684\uff09<\/p>\n<\/blockquote>\n<p>\u793a\u4f8b1\uff1a\u7ed9\u5b57\u7b26\u4e32String\u7c7b\u6dfb\u52a0\u4e0b\u6807\u811a\u672c\uff0c\u652f\u6301\u7d22\u5f15\u8bbf\u95ee<\/p>\n<pre><code class=\"language-swift\">extension String\n{\n    subscript(start:Int, length:Int) -&gt; String {\n        get {\n            let index1 = self.index(self.startIndex, offsetBy: start)\n            let index2 = self.index(index1, offsetBy: length)\n            return String(self[index1..&lt;index2])\n        }\n        set {\n            let tmp = self\n            var s = &quot;&quot;\n            var e = &quot;&quot;\n            for (idx, item) in tmp.characters.enumerated() {\n                if(idx &lt; start) {\n                    s += &quot;\\(item)&quot;\n                }\n                if(idx &gt;= start + length) {\n                    e += &quot;\\(item)&quot;\n                }\n            }\n            self = s + newValue + e\n        }\n    }\n    subscript(index:Int) -&gt; String {\n        get {\n            return String(self[self.index(self.startIndex, offsetBy: index)])\n        }\n        set {\n            let tmp = self\n            self = &quot;&quot;\n            for (idx, item) in tmp.characters.enumerated() {\n                if idx == index {\n                    self += &quot;\\(newValue)&quot;\n                } else {\n                    self += &quot;\\(item)&quot;\n                }\n            }\n        }\n    }\n}\n\nlet str = SubString(str:&quot;appblog.cn&quot;)\nprint(str[8,2])  \/\/\u83b7\u53d6\u5b57\u7b26\u4e32\uff1acn\nprint(str[8])  \/\/\u83b7\u53d6\u5b57\u7b26\uff1ac\n\nstr[8,2] = &quot;CN&quot;  \/\/\u8bbe\u7f6e\u90e8\u5206\u5b57\u7b26\u4e32\nstr[0] = &quot;A&quot;  \/\/\u8bbe\u7f6e\u90e8\u5206\u5b57\u7b26\nprint(str[0,10])  \/\/Appblog.CN<\/code><\/pre>\n<p>\u793a\u4f8b2\uff1a\u7ed9Double\u589e\u52a0mm\uff0ccm\u7b49\u65b9\u6cd5\uff0c\u8fdb\u884c\u8fdb\u5236\u8f6c\u6362<\/p>\n<pre><code class=\"language-swift\">extension Double\n{\n    func mm()-&gt;String {\n        return &quot;\\(self\/1)mm&quot;\n    }\n    func cm()-&gt; String {\n       return &quot;\\(self\/10)cm&quot;\n    }\n\n    func dm()-&gt;String {\n        return &quot;\\(self\/100)dm&quot;\n    }\n\n    func m()-&gt;String {\n        return &quot;\\(self\/1000)m&quot;\n    }\n    func km()-&gt;String {\n        return &quot;\\(self\/(1000*1000))km&quot;\n    }\n}\n\nlet value = 2000000000.0\nprint(value.mm())\nprint(value.cm())\nprint(value.dm())\nprint(value.m())\nprint(value.km())<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4 Swift\u8bed\u8a00\u7684\u7c7b\u6269\u5c55\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7c7b\u6269\u5c55\u5b8c\u6210\u5982\u4e0b\u4e8b\u60c5\uff1a \u7ed9\u5df2\u6709 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[171],"tags":[],"class_list":["post-568","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/568","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=568"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/568\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}