{"id":566,"date":"2023-02-25T19:10:56","date_gmt":"2023-02-25T11:10:56","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=566"},"modified":"2023-04-29T20:34:51","modified_gmt":"2023-04-29T12:34:51","slug":"swift-introduction-and-example-of-subscript-scripting-method","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/swift-introduction-and-example-of-subscript-scripting-method\/","title":{"rendered":"Swift &#8211; \u4e0b\u6807\u811a\u672c\u65b9\u6cd5\u4ecb\u7ecd\u53ca\u5b9e\u4f8b"},"content":{"rendered":"<blockquote>\n<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4<\/p>\n<\/blockquote>\n<p>\u5b9a\u4e49\u4e0b\u6807\u811a\u672c\u4e4b\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u201c[]\u201d\u6765\u5b58\u53d6\u6570\u636e\u7c7b\u578b\u7684\u503c\u3002<\/p>\n<p>\u793a\u4f8b1\uff1a\u5b9e\u73b0\u4e00\u4e2a\u6211\u4eec\u81ea\u5b9a\u7684\u5b57\u7b26\u4e32\u7c7b\uff0c\u53ef\u4ee5\u65b9\u4fbf\u7684\u901a\u8fc7\u7d22\u5f15\u83b7\u53d6\u67d0\u4e00\u4e2a\u5b57\u7b26\u503c\uff0c\u6216\u67d0\u4e00\u90e8\u5206\u5b57\u7b26\u4e32\u3002\u540c\u65f6\u4e5f\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\uff0c\u7ed9\u67d0\u4e00\u90e8\u5206\u8d4b\u503c\u3002<\/p>\n<p><!-- more --><\/p>\n<pre><code class=\"language-swift\">class SubString\n{\n    var str:String = &quot;&quot;\n    init(str:String)\n    {\n        self.str = str;\n    }\n\n    \/**\u4e0b\u6807\u811a\u672c\uff1a\u83b7\u53d6\/\u8bbe\u7f6e\u90e8\u5206\u5b57\u7b26\u4e32**\/\n    subscript(start:Int, length:Int) -&gt; String {\n        get {\n            let index1 = str.index(str.startIndex, offsetBy: start)\n            let index2 = str.index(index1, offsetBy: length)\n            let range = Range(uncheckedBounds: (lower: index1, upper: index2))\n            return str.substring(with: range)\n        }\n        set {\n            let tmp = str\n            str = &quot;&quot;\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            str = s + newValue + e\n        }\n    }\n\n    \/**\u4e0b\u6807\u811a\u672c\uff1a\u83b7\u53d6\/\u8bbe\u7f6e\u5b57\u7b26**\/\n    subscript(index:Int) -&gt; String {\n        get {\n            return String(str[str.index(str.startIndex, offsetBy: index)])\n        }\n        set {\n            let tmp = str\n            str = &quot;&quot;\n            for (idx, item) in tmp.characters.enumerated() {\n                if idx == index {\n                    str += &quot;\\(newValue)&quot;\n                } else {\n                    str += &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\u4f8b1\u6539\u8fdb\uff1a\u901a\u8fc7\u7c7b\u6269\u5c55\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u7ed9String\u7c7b\u6dfb\u52a0\u7d22\u5f15\u529f\u80fd\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/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            let range = Range(uncheckedBounds: (lower: index1, upper: index2))\n            return self.substring(with: range)\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\u4f7f\u7528\u4e00\u7ef4\u6570\u7ec4\u7ed3\u5408\u4e0b\u6807\u65b9\u6cd5\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u6a21\u62df\u5b9e\u73b0\u4e86\u4e8c\u7ef4\u6570\u7ec4<\/p>\n<pre><code class=\"language-swift\">class Matrix {\n    let rows: Int, columns: Int\n    var grid: [Double]\n\n    init(rows: Int, columns: Int) {\n        self.rows = rows\n        self.columns = columns\n        grid = Array(repeating: 0.0, count: rows * columns)\n    }\n\n    func indexIsValidForRow(row: Int, column: Int) -&gt; Bool {\n        return row &gt;= 0 &amp;&amp; row &lt; rows &amp;&amp; column &gt;= 0 &amp;&amp; column &lt; columns\n    }\n\n    subscript(row: Int, column: Int) -&gt; Double {\n        get {\n            assert(indexIsValidForRow(row: row, column: column), &quot;Index out of range&quot;)\n            return grid[(row * columns) + column]\n        }\n        set {\n            assert(indexIsValidForRow(row: row, column: column), &quot;Index out of range&quot;)\n            grid[(row * columns) + column] = newValue\n        }\n    }\n}\n\nlet value = Matrix(rows: 20, columns: 20)\nvalue[10, 10] = 20\nprint(value[10, 10])<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4 \u5b9a\u4e49\u4e0b\u6807\u811a\u672c\u4e4b\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u201c[]\u201d\u6765\u5b58\u53d6\u6570\u636e\u7c7b\u578b\u7684\u503c\u3002 \u793a\u4f8b1\uff1a\u5b9e\u73b0\u4e00\u4e2a\u6211\u4eec\u81ea\u5b9a [&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-566","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/566","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=566"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/566\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}