{"id":529,"date":"2018-02-23T23:39:01","date_gmt":"2018-02-23T15:39:01","guid":{"rendered":"http:\/\/60.205.208.74\/2018\/02\/23\/swift-%e5%ad%97%e7%ac%a6%e4%b8%b2\/"},"modified":"2018-02-23T23:39:01","modified_gmt":"2018-02-23T15:39:01","slug":"swift-%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/www.puzi7.net\/?p=529","title":{"rendered":"Swift \u5b57\u7b26\u4e32"},"content":{"rendered":"<span itemprop=\"description\"><div class=\"article-body\">\n<div class=\"article-intro\" id=\"content\">\n<h1>Swift \u5b57\u7b26\u4e32<\/h1>\n<p>Swift \u5b57\u7b26\u4e32\u662f\u4e00\u7cfb\u5217\u5b57\u7b26\u7684\u96c6\u5408\u3002\u4f8b\u5982 &#8220;Hello, World!&#8221; \u8fd9\u6837\u7684\u6709\u5e8f\u7684\u5b57\u7b26\u7c7b\u578b\u7684\u503c\u7684\u96c6\u5408\uff0c\u5b83\u7684\u6570\u636e\u7c7b\u578b\u4e3a <b>String<\/b>\u3002\n<\/p>\n<hr>\n<h2>\u521b\u5efa\u5b57\u7b26\u4e32<\/h2>\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u6216 String \u7c7b\u7684\u5b9e\u4f8b\u6765\u521b\u5efa\u4e00\u4e2a\u5b57\u7b26\u4e32\uff1a<\/p>\n<pre>\nimport Cocoa\n\n\/\/ \u4f7f\u7528\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\nvar stringA = \"Hello, World!\"\nprint( stringA )\n\n\/\/ String \u5b9e\u4f8b\u5316\nvar stringB = String(\"Hello, World!\")\nprint( stringB )\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\nHello, World!\nHello, World!\n<\/pre>\n<hr>\n<h2>\u7a7a\u5b57\u7b26\u4e32<\/h2>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u7a7a\u7684\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u8d4b\u503c\u7ed9\u53d8\u91cf\u6216\u521d\u59cb\u5316\u4e00\u4e2aString\u7c7b\u7684\u5b9e\u4f8b\u6765\u521d\u59cb\u503c\u4e00\u4e2a\u7a7a\u7684\u5b57\u7b26\u4e32\u3002<br \/>\n\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u5c5e\u6027 isEmpty \u6765\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u7a7a\uff1a<\/p>\n<pre>\nimport Cocoa\n\n\/\/ \u4f7f\u7528\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u521b\u5efa\u7a7a\u5b57\u7b26\u4e32\nvar stringA = \"\"\n\nif stringA.isEmpty {\n   print( \"stringA \u662f\u7a7a\u7684\" )\n} else {\n   print( \"stringA \u4e0d\u662f\u7a7a\u7684\" )\n}\n\n\/\/ \u5b9e\u4f8b\u5316 String \u7c7b\u6765\u521b\u5efa\u7a7a\u5b57\u7b26\u4e32\nlet stringB = String()\n\nif stringB.isEmpty {\n   print( \"stringB \u662f\u7a7a\u7684\" )\n} else {\n   print( \"stringB \u4e0d\u662f\u7a7a\u7684\" )\n}\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\nstringA \u662f\u7a7a\u7684\nstringB \u662f\u7a7a\u7684\n<\/pre>\n<hr>\n<h2>\u5b57\u7b26\u4e32\u5e38\u91cf<\/h2>\n<p>\u4f60\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\u6216\u5e38\u91cf\uff0c\u53d8\u91cf\u662f\u53ef\u4fee\u6539\u7684\uff0c\u5e38\u91cf\u662f\u4e0d\u53ef\u4fee\u6539\u7684\u3002<\/p>\n<pre>\nimport Cocoa\n\n\/\/ stringA \u53ef\u88ab\u4fee\u6539\nvar stringA = \"\u83dc\u9e1f\u6559\u7a0b\uff1a\"\nstringA += \"http:\/\/www.runoob.com\"\nprint( stringA )\n\n\/\/ stringB \u4e0d\u80fd\u4fee\u6539\nlet stringB = String(\"\u83dc\u9e1f\u6559\u7a0b\uff1a\")\nstringB += \"http:\/\/www.runoob.com\"\nprint( stringB )\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4f1a\u62a5\u9519\uff0c\u4ee5\u4e3a stringB \u4e3a\u5e38\u91cf\u662f\u4e0d\u80fd\u88ab\u4fee\u6539\u7684\uff1a<\/p>\n<pre>\nerror: left side of mutating operator isn't mutable: 'stringB' is a 'let' constant\nstringB += \"http:\/\/www.runoob.com\"\n<\/pre>\n<hr>\n<h2>\u5b57\u7b26\u4e32\u4e2d\u63d2\u5165\u503c<\/h2>\n<p>\u5b57\u7b26\u4e32\u63d2\u503c\u662f\u4e00\u79cd\u6784\u5efa\u65b0\u5b57\u7b26\u4e32\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u5728\u5176\u4e2d\u5305\u542b\u5e38\u91cf\u3001\u53d8\u91cf\u3001\u5b57\u9762\u91cf\u548c\u8868\u8fbe\u5f0f\u3002 \u60a8\u63d2\u5165\u7684\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u7684\u6bcf\u4e00\u9879\u90fd\u5728\u4ee5\u53cd\u659c\u7ebf\u4e3a\u524d\u7f00\u7684\u5706\u62ec\u53f7\u4e2d\uff1a<\/p>\n<pre>\nimport Cocoa\n\nvar varA   = 20\nlet constA = 100\nvar varC:Float = 20.0\n\nvar stringA = \"\\(varA) \u4e58\u4e8e \\(constA) \u7b49\u4e8e \\(varC * 100)\"\nprint( stringA )\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>20 \u4e58\u4e8e 100 \u7b49\u4e8e 2000.0<\/pre>\n<hr>\n<h2>\u5b57\u7b26\u4e32\u8fde\u63a5<\/h2>\n<p>\u5b57\u7b26\u4e32\u53ef\u4ee5\u901a\u8fc7 <b>+<\/b> \u53f7\u6765\u8fde\u63a5\uff0c\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre>\nimport Cocoa\n\nlet constA = \"\u83dc\u9e1f\u6559\u7a0b\uff1a\"\nlet constB = \"http:\/\/www.runoob.com\"\n\nvar stringA = constA + constB\n\nprint( stringA )\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n\u83dc\u9e1f\u6559\u7a0b\uff1ahttp:\/\/www.runoob.com\n<\/pre>\n<hr>\n<h2>\u5b57\u7b26\u4e32\u957f\u5ea6<\/h2>\n<p>\u5b57\u7b26\u4e32\u957f\u5ea6\u4f7f\u7528 <b>String.characters.count<\/b> \u5c5e\u6027\u6765\u8ba1\u7b97\uff0c\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre>\nimport Cocoa\n\nvar varA   = \"www.runoob.com\"\n\nprint( \"\\(varA), \u957f\u5ea6\u4e3a \\(varA.characters.count)\" )\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\nwww.runoob.com, \u957f\u5ea6\u4e3a 14\n<\/pre>\n<hr>\n<h2>\u5b57\u7b26\u4e32\u6bd4\u8f83<\/h2>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 <b>==<\/b> \u6765\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u76f8\u7b49\uff1a<\/p>\n<pre>\nimport Cocoa\n\nvar varA   = \"Hello, Swift!\"\nvar varB   = \"Hello, World!\"\n\nif varA == varB {\n   print( \"\\(varA) \u4e0e \\(varB) \u662f\u76f8\u7b49\u7684\" )\n} else {\n   print( \"\\(varA) \u4e0e \\(varB) \u662f\u4e0d\u76f8\u7b49\u7684\" )\n}\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\nHello, Swift! \u4e0e Hello, World! \u662f\u4e0d\u76f8\u7b49\u7684\n<\/pre>\n<hr>\n<h2>Unicode \u5b57\u7b26\u4e32<\/h2>\n<p>Unicode \u662f\u4e00\u4e2a\u56fd\u9645\u6807\u51c6\uff0c\u7528\u4e8e\u6587\u672c\u7684\u7f16\u7801\uff0cSwift \u7684 String \u7c7b\u578b\u662f\u57fa\u4e8e Unicode\u5efa\u7acb\u7684\u3002\u4f60\u53ef\u4ee5\u5faa\u73af\u8fed\u4ee3\u51fa\u5b57\u7b26\u4e32\u4e2d UTF-8 \u4e0e UTF-16 \u7684\u7f16\u7801\uff0c\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre>\nimport Cocoa\n\nvar unicodeString   = \"\u83dc\u9e1f\u6559\u7a0b\"\n\nprint(\"UTF-8 \u7f16\u7801: \")\nfor code in unicodeString.utf8 {\n   print(\"\\(code) \")\n}\n\nprint(\"\\n\")\n\nprint(\"UTF-16 \u7f16\u7801: \")\nfor code in unicodeString.utf16 {\n   print(\"\\(code) \")\n}\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\nUTF-8 \u7f16\u7801: \n232 \n143 \n156 \n233 \n184 \n159 \n230 \n149 \n153 \n231 \n168 \n139 \nUTF-16 \u7f16\u7801: \n33756 \n40479 \n25945 \n31243 \n<\/pre>\n<hr>\n<h2>\u5b57\u7b26\u4e32\u51fd\u6570\u53ca\u8fd0\u7b97\u7b26<\/h2>\n<p>Swift  \u652f\u6301\u4ee5\u4e0b\u51e0\u79cd\u5b57\u7b26\u4e32\u51fd\u6570\u53ca\u8fd0\u7b97\u7b26\uff1a<\/p>\n<table class=\"reference\">\n<tbody>\n<tr>\n<th>\u5e8f\u53f7<\/th>\n<th>\u51fd\u6570\/\u8fd0\u7b97\u7b26 &amp; \u63cf\u8ff0<\/th>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>\n<p><b>isEmpty<\/b><\/p>\n<p>\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u4e3a\u7a7a\uff0c\u8fd4\u56de\u5e03\u5c14\u503c<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>\n<p><b>hasPrefix(prefix: String)<\/b><\/p>\n<p>\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u62e5\u6709\u7279\u5b9a\u524d\u7f00<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>\n<p><b>hasSuffix(suffix: String)<\/b><\/p>\n<p>\u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u62e5\u6709\u7279\u5b9a\u540e\u7f00\u3002<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>\n<p><b>Int(String)<\/b><\/p>\n<p>\u8f6c\u6362\u5b57\u7b26\u4e32\u6570\u5b57\u4e3a\u6574\u578b\u3002<br \/>\n\u5b9e\u4f8b:\n<\/p>\n<pre>\nlet myString: String = \"256\"\nlet myInt: Int? = Int(myString)\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>\n<p><b>String.characters.count<\/b><\/p>\n<p>\u8ba1\u7b97\u5b57\u7b26\u4e32\u7684\u957f\u5ea6<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>\n<p><b>utf8<\/b><\/p>\n<p>\u60a8\u53ef\u4ee5\u901a\u8fc7\u904d\u5386 String \u7684 utf8 \u5c5e\u6027\u6765\u8bbf\u95ee\u5b83\u7684 UTF-8 \u7f16\u7801<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>\n<p><b>utf16<\/b><\/p>\n<p>\u60a8\u53ef\u4ee5\u901a\u8fc7\u904d\u5386 String \u7684 utf8 \u5c5e\u6027\u6765\u8bbf\u95ee\u5b83\u7684 UTF-16 \u7f16\u7801<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>8<\/td>\n<td>\n<p><b>unicodeScalars<\/b><\/p>\n<p>\u60a8\u53ef\u4ee5\u901a\u8fc7\u904d\u5386String\u503c\u7684unicodeScalars\u5c5e\u6027\u6765\u8bbf\u95ee\u5b83\u7684 Unicode \u6807\u91cf\u7f16\u7801.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>9<\/td>\n<td>\n<p><b>+<\/b><\/p>\n<p>\u8fde\u63a5\u4e24\u4e2a\u5b57\u7b26\u4e32\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>10<\/td>\n<td>\n<p><b>+=<\/b><\/p>\n<p>\u8fde\u63a5\u64cd\u4f5c\u7b26\u4e24\u8fb9\u7684\u5b57\u7b26\u4e32\u5e76\u5c06\u65b0\u5b57\u7b26\u4e32\u8d4b\u503c\u7ed9\u5de6\u8fb9\u7684\u64cd\u4f5c\u7b26\u53d8\u91cf<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>11<\/td>\n<td>\n<p><b>==<\/b><\/p>\n<p>\u5224\u65ad\u4e24\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u76f8\u7b49<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>12<\/td>\n<td>\n<p><b>&lt;<\/b><\/p>\n<p>\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\uff0c\u5bf9\u4e24\u4e2a\u5b57\u7b26\u4e32\u7684\u5b57\u6bcd\u9010\u4e00\u6bd4\u8f83\u3002<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>13<\/td>\n<td>\n<p><b>!=<\/b><\/p>\n<p>\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u4e0d\u76f8\u7b49\u3002<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table><\/div>\n<\/p><\/div>\n<span>","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\"><span itemprop=\"description\">Swift \u5b57\u7b26\u4e32 Swift \u5b57\u7b26\u4e32\u662f\u4e00\u7cfb\u5217\u5b57\u7b26\u7684\u96c6\u5408\u3002\u4f8b\u5982 &#8220;Hello,&hellip;<\/span><\/p>\n<p class=\"more-link-p btn-align-right\"><a class=\"green zoom-btn\" href=\"https:\/\/www.puzi7.net\/?p=529\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,28],"tags":[74,119],"class_list":["post-529","post","type-post","status-publish","format-standard","hentry","category-mobile","category-swift","tag-ios","tag-swift","zoom-theme-has-thumb"],"_links":{"self":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=529"}],"version-history":[{"count":0,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/529\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}