{"id":175214,"date":"2020-04-07T11:34:26","date_gmt":"2020-04-07T03:34:26","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=175214"},"modified":"2020-03-24T12:36:10","modified_gmt":"2020-03-24T04:36:10","slug":"swift-character-tutorial","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/swift-character-tutorial.html","title":{"rendered":"Swift \u5b57\u7b26(Character)\u8bb2\u89e3"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nSwift \u662f\u4e00\u79cd\u652f\u6301\u591a\u7f16\u7a0b\u8303\u5f0f\u548c\u7f16\u8bd1\u5f0f\u7684\u5f00\u6e90\u7f16\u7a0b\u8bed\u8a00,\u82f9\u679c\u4e8e2014\u5e74WWDC\uff08\u82f9\u679c\u5f00\u53d1\u8005\u5927\u4f1a\uff09\u53d1\u5e03\uff0c\u7528\u4e8e\u5f00\u53d1 iOS\uff0cOS X \u548c watchOS \u5e94\u7528\u7a0b\u5e8f\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\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

\u4ee5\u4e0b\u5b9e\u4f8b\u5217\u51fa\u4e86\u4e24\u4e2a\u5b57\u7b26\u5b9e\u4f8b\uff1a<\/p>\n

\r\nimport Cocoa\r\n\r\nlet char1: Character = \"A\"\r\nlet char2: Character = \"B\"\r\n\r\nprint(\"char1 \u7684\u503c\u4e3a \\(char1)\")\r\nprint(\"char2 \u7684\u503c\u4e3a \\(char2)\")\r\n<\/pre>\n

\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n

\r\nchar1 \u7684\u503c\u4e3a A\r\nchar2 \u7684\u503c\u4e3a B<\/pre>\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

\r\nimport Cocoa\r\n\r\n\/\/ Swift \u4e2d\u4ee5\u4e0b\u8d4b\u503c\u4f1a\u62a5\u9519\r\nlet char: Character = \"AB\"\r\n\r\nprint(\"Value of char \\(char)\")<\/pre>\n

\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n

\r\nerror: cannot convert value of type 'String' to specified type 'Character'\r\nlet char: Character = \"AB\"<\/pre>\n
\u7a7a\u5b57\u7b26\u53d8\u91cf<\/strong><\/div>\n

Swift \u4e2d\u4e0d\u80fd\u521b\u5efa\u7a7a\u7684 Character\uff08\u5b57\u7b26\uff09 \u7c7b\u578b\u53d8\u91cf\u6216\u5e38\u91cf\uff1a<\/p>\n

\r\nimport Cocoa\r\n\r\n\/\/ Swift \u4e2d\u4ee5\u4e0b\u8d4b\u503c\u4f1a\u62a5\u9519\r\nlet char1: Character = \"\"\r\nvar char2: Character = \"\"\r\n\r\nprint(\"char1 \u7684\u503c\u4e3a \\(char1)\")\r\nprint(\"char2 \u7684\u503c\u4e3a \\(char2)\")<\/pre>\n

\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n

\r\n error: cannot convert value of type 'String' to specified type 'Character'\r\nlet char1: Character = \"\"\r\n                       ^~\r\nerror: cannot convert value of type 'String' to specified type 'Character'\r\nvar char2: Character = \"\"<\/pre>\n
\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26<\/strong><\/div>\n

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

Swift 3 \u4e2d\u7684 String \u9700\u8981\u901a\u8fc7 characters \u53bb\u8c03\u7528\u7684\u5c5e\u6027\u65b9\u6cd5\uff0c\u5728 Swift 4 \u4e2d\u53ef\u4ee5\u901a\u8fc7 String \u5bf9\u8c61\u672c\u8eab\u76f4\u63a5\u8c03\u7528\uff0c\u4f8b\u5982\uff1a<\/p>\n

Swift 3 \u4e2d\uff1a<\/p>\n

\r\nimport Cocoa\r\n\r\nfor ch in \"Runoob\".characters {\r\n    print(ch)\r\n}<\/pre>\n

Swift 4 \u4e2d\uff1a<\/p>\n

\r\nimport Cocoa\r\n\r\nfor ch in \"Runoob\" {\r\n    print(ch)\r\n}<\/pre>\n

\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n

\r\nR\r\nu\r\nn\r\no\r\no\r\nb<\/pre>\n
\u5b57\u7b26\u4e32\u8fde\u63a5\u5b57\u7b26<\/strong><\/div>\n

\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

\r\nimport Cocoa\r\n\r\nvar varA:String = \"Hello \"\r\nlet varB:Character = \"G\"\r\n\r\nvarA.append( varB )\r\n\r\nprint(\"varC  =  \\(varA)\")<\/pre>\n

\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n

\r\nvarC  =  Hello G<\/pre>\n","protected":false},"excerpt":{"rendered":"

Swift \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 \u4ee5\u4e0b\u5b9e\u4f8b\u5217\u51fa\u4e86\u4e24\u4e2a\u5b57\u7b26\u5b9e\u4f8b\uff1a […]<\/p>\n","protected":false},"author":1470,"featured_media":175141,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-175214","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/175214","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1470"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=175214"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/175214\/revisions"}],"predecessor-version":[{"id":175222,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/175214\/revisions\/175222"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/175141"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=175214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=175214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=175214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}