{"id":530,"date":"2018-02-23T23:39:02","date_gmt":"2018-02-23T15:39:02","guid":{"rendered":"http:\/\/60.205.208.74\/2018\/02\/23\/swift-%e5%ad%97%e7%ac%a6character\/"},"modified":"2018-02-23T23:39:02","modified_gmt":"2018-02-23T15:39:02","slug":"swift-%e5%ad%97%e7%ac%a6character","status":"publish","type":"post","link":"https:\/\/www.puzi7.net\/?p=530","title":{"rendered":"Swift \u5b57\u7b26(Character)"},"content":{"rendered":"<span itemprop=\"description\"><div class=\"article-body\">\n<div class=\"article-intro\" id=\"content\">\n<h1>Swift \u5b57\u7b26(Character)<\/h1>\n<p>\nSwift \u7684\u5b57\u7b26\u662f\u4e00\u4e2a\u5355\u4e00\u7684\u5b57\u7b26\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff0c\u6570\u636e\u7c7b\u578b\u4e3a Character\u3002<\/p>\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u5217\u51fa\u4e86\u4e24\u4e2a\u5b57\u7b26\u5b9e\u4f8b\uff1a<\/p>\n<pre>\nimport Cocoa\n\nlet char1: Character = \"A\"\nlet char2: Character = \"B\"\n\nprint(\"char1 \u7684\u503c\u4e3a \\(char1)\")\nprint(\"char2 \u7684\u503c\u4e3a \\(char2)\")\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>char1 \u7684\u503c\u4e3a A\nchar2 \u7684\u503c\u4e3a B<\/pre>\n<p>\n\u5982\u679c\u4f60\u60f3\u5728 Character\uff08\u5b57\u7b26\uff09 \u7c7b\u578b\u7684\u5e38\u91cf\u4e2d\u5b58\u50a8\u66f4\u591a\u7684\u5b57\u7b26\uff0c\u5219\u7a0b\u5e8f\u6267\u884c\u4f1a\u62a5\u9519\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>\nimport Cocoa\n\n\/\/ Swift \u4e2d\u4ee5\u4e0b\u8d4b\u503c\u4f1a\u62a5\u9519\nlet char: Character = \"AB\"\n\nprint(\"Value of char \\(char)\")\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\nerror: cannot convert value of type 'String' to specified type 'Character'\nlet char: Character = \"AB\"\n<\/pre>\n<hr>\n<h2>\u7a7a\u5b57\u7b26\u53d8\u91cf<\/h2>\n<p>Swift \u4e2d\u4e0d\u80fd\u521b\u5efa\u7a7a\u7684 Character\uff08\u5b57\u7b26\uff09 \u7c7b\u578b\u53d8\u91cf\u6216\u5e38\u91cf\uff1a<\/p>\n<pre>\nimport Cocoa\n\n\/\/ Swift \u4e2d\u4ee5\u4e0b\u8d4b\u503c\u4f1a\u62a5\u9519\nlet char1: Character = \"\"\nvar char2: Character = \"\"\n\nprint(\"char1 \u7684\u503c\u4e3a \\(char1)\")\nprint(\"char2 \u7684\u503c\u4e3a \\(char2)\")\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n error: cannot convert value of type 'String' to specified type 'Character'\nlet char1: Character = \"\"\n                       ^~\nerror: cannot convert value of type 'String' to specified type 'Character'\nvar char2: Character = \"\"\n<\/pre>\n<hr>\n<h2>\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26<\/h2>\n<p>Swift \u7684 String \u7c7b\u578b\u8868\u793a\u7279\u5b9a\u5e8f\u5217\u7684 Character\uff08\u5b57\u7b26\uff09 \u7c7b\u578b\u503c\u7684\u96c6\u5408\u3002 \u6bcf\u4e00\u4e2a\u5b57\u7b26\u503c\u4ee3\u8868\u4e00\u4e2a Unicode \u5b57\u7b26\u3002<\/p>\n<p>\u60a8\u53ef\u901a\u8fc7for-in\u5faa\u73af\u6765\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684characters\u5c5e\u6027\u6765\u83b7\u53d6\u6bcf\u4e00\u4e2a\u5b57\u7b26\u7684\u503c\uff1a<\/p>\n<pre>\nimport Cocoa\n\nfor ch in \"Runoob\".characters {\n    print(ch)\n}\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\nR\nu\nn\no\no\nb\n<\/pre>\n<hr>\n<h2>\u5b57\u7b26\u4e32\u8fde\u63a5\u5b57\u7b26<\/h2>\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u6f14\u793a\u4e86\u4f7f\u7528 String \u7684 append() \u65b9\u6cd5\u6765\u5b9e\u73b0\u5b57\u7b26\u4e32\u8fde\u63a5\u5b57\u7b26\uff1a<\/p>\n<pre>\nimport Cocoa\n\nvar varA:String = \"Hello \"\nlet varB:Character = \"G\"\n\nvarA.append( varB )\n\nprint(\"varC  =  \\(varA)\")\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\nvarC  =  Hello G\n<\/pre>\n<\/p><\/div>\n<\/p><\/div>\n<span>","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\"><span itemprop=\"description\">Swift \u5b57\u7b26(Character) Swift \u7684\u5b57\u7b26\u662f\u4e00\u4e2a\u5355\u4e00\u7684\u5b57\u7b26\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff0c\u6570&hellip;<\/span><\/p>\n<p class=\"more-link-p btn-align-right\"><a class=\"green zoom-btn\" href=\"https:\/\/www.puzi7.net\/?p=530\">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-530","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\/530","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=530"}],"version-history":[{"count":0,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/530\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=530"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}