{"id":175135,"date":"2020-04-11T08:25:32","date_gmt":"2020-04-11T00:25:32","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=175135"},"modified":"2020-03-24T12:26:13","modified_gmt":"2020-03-24T04:26:13","slug":"swift-variable-explanation","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/swift-variable-explanation.html","title":{"rendered":"Swift \u53d8\u91cf\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

\"\"
\n\u53d8\u91cf\u662f\u4e00\u79cd\u4f7f\u7528\u65b9\u4fbf\u7684\u5360\u4f4d\u7b26\uff0c\u7528\u4e8e\u5f15\u7528\u8ba1\u7b97\u673a\u5185\u5b58\u5730\u5740\u3002<\/p>\n

Swift \u6bcf\u4e2a\u53d8\u91cf\u90fd\u6307\u5b9a\u4e86\u7279\u5b9a\u7684\u7c7b\u578b\uff0c\u8be5\u7c7b\u578b\u51b3\u5b9a\u4e86\u53d8\u91cf\u5360\u7528\u5185\u5b58\u7684\u5927\u5c0f\uff0c\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u4e5f\u51b3\u5b9a\u53ef\u5b58\u50a8\u503c\u7684\u8303\u56f4\u3002<\/p>\n

\u4e0a\u4e00\u7ae0\u8282\u6211\u4eec\u5df2\u7ecf\u4e3a\u5927\u5bb6\u4ecb\u7ecd\u4e86\u57fa\u672c\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u6574\u5f62Int\u3001\u6d6e\u70b9\u6570Double\u548cFloat\u3001\u5e03\u5c14\u7c7b\u578bBool\u4ee5\u53ca\u5b57\u7b26\u4e32\u7c7b\u578bString\u3002\u6b64\u5916\uff0cSwift\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u66f4\u5f3a\u5927\u6570\u636e\u7c7b\u578b\uff0c Optional, Array, Dictionary, Struct, \u548c Class \u7b49\u3002<\/p>\n

\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u4e3a\u5927\u5bb6\u4ecb\u7ecd\u5982\u4f55\u5728 Swift \u7a0b\u5e8f\u4e2d\u58f0\u660e\u548c\u4f7f\u7528\u53d8\u91cf\u3002<\/p>\n

\u53d8\u91cf\u58f0\u660e<\/strong><\/div>\n

\u53d8\u91cf\u58f0\u660e\u610f\u601d\u662f\u544a\u8bc9\u7f16\u8bd1\u5668\u5728\u5185\u5b58\u4e2d\u7684\u54ea\u4e2a\u4f4d\u7f6e\u4e0a\u4e3a\u53d8\u91cf\u521b\u5efa\u591a\u5927\u7684\u5b58\u50a8\u7a7a\u95f4\u3002
\n\u5728\u4f7f\u7528\u53d8\u91cf\u524d\uff0c\u4f60\u9700\u8981\u4f7f\u7528 var \u5173\u952e\u5b57\u58f0\u660e\u5b83\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

var variableName = (initial value)<\/pre>\n

\u4ee5\u4e0b\u662f\u4e00\u4e2a Swift \u7a0b\u5e8f\u4e2d\u53d8\u91cf\u58f0\u660e\u7684\u7b80\u5355\u5b9e\u4f8b\uff1a<\/p>\n

import Cocoa\r\n\r\nvar varA = 42\r\nprint(varA)\r\n\r\nvar varB:Float\r\n\r\nvarB = 3.14159\r\nprint(varB)<\/pre>\n

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

42\r\n3.14159<\/pre>\n
\u53d8\u91cf\u547d\u540d<\/strong><\/div>\n

\u53d8\u91cf\u540d\u53ef\u4ee5\u7531\u5b57\u6bcd\uff0c\u6570\u5b57\u548c\u4e0b\u5212\u7ebf\u7ec4\u6210\u3002<\/p>\n

\u53d8\u91cf\u540d\u9700\u8981\u4ee5\u5b57\u6bcd\u6216\u4e0b\u5212\u7ebf\u5f00\u59cb\u3002<\/p>\n

Swift \u662f\u4e00\u4e2a\u533a\u5206\u5927\u5c0f\u5199\u7684\u8bed\u8a00\uff0c\u6240\u4ee5\u5b57\u6bcd\u5927\u5199\u4e0e\u5c0f\u5199\u662f\u4e0d\u4e00\u6837\u7684\u3002<\/p>\n

\u53d8\u91cf\u540d\u4e5f\u53ef\u4ee5\u4f7f\u7528\u7b80\u5355\u7684 Unicode \u5b57\u7b26\uff0c\u5982\u4e0b\u5b9e\u4f8b\uff1a<\/p>\n

\r\nimport Cocoa\r\n\r\nvar _var = \"Hello, Swift!\"\r\nprint(_var)\r\n\r\nvar \u4f60\u597d = \"\u4f60\u597d\u4e16\u754c\"\r\nvar \u83dc\u9e1f\u6559\u7a0b = \"www.runoob.com\"\r\nprint(\u4f60\u597d)\r\nprint(\u83dc\u9e1f\u6559\u7a0b)<\/pre>\n

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

\r\nHello, Swift!\r\n\u4f60\u597d\u4e16\u754c\r\nwww.runoob.com\r\n<\/pre>\n
\u53d8\u91cf\u8f93\u51fa<\/strong><\/div>\n

\u53d8\u91cf\u548c\u5e38\u91cf\u53ef\u4ee5\u4f7f\u7528 print\uff08swift 2 \u5c06 print \u66ff\u6362\u4e86 println\uff09 \u51fd\u6570\u6765\u8f93\u51fa\u3002<\/p>\n

\u5728\u5b57\u7b26\u4e32\u4e2d\u53ef\u4ee5\u4f7f\u7528\u62ec\u53f7\u4e0e\u53cd\u659c\u7ebf\u6765\u63d2\u5165\u53d8\u91cf\uff0c\u5982\u4e0b\u5b9e\u4f8b\uff1a<\/p>\n

\r\nimport Cocoa\r\n\r\nvar name = \"Linux\u5c31\u8be5\u8fd9\u4e48\u5b66\"\r\nvar site = \"http:\/\/lrxjmw.cn\"\r\n\r\nprint(\"\\(name)\u7684\u5b98\u7f51\u5730\u5740\u4e3a\uff1a\\(site)\")<\/pre>\n

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

\r\nLinux\u5c31\u8be5\u8fd9\u4e48\u5b66\u5b98\u7f51\u5730\u5740\u4e3a\uff1ahttp:\/\/lrxjmw.cn<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u53d8\u91cf\u662f\u4e00\u79cd\u4f7f\u7528\u65b9\u4fbf\u7684\u5360\u4f4d\u7b26\uff0c\u7528\u4e8e\u5f15\u7528\u8ba1\u7b97\u673a\u5185\u5b58\u5730\u5740\u3002 Swift \u6bcf\u4e2a\u53d8\u91cf\u90fd\u6307\u5b9a\u4e86\u7279\u5b9a\u7684\u7c7b\u578b\uff0c\u8be5\u7c7b\u578b\u51b3\u5b9a\u4e86\u53d8\u91cf […]<\/p>\n","protected":false},"author":321,"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-175135","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\/175135","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\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=175135"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/175135\/revisions"}],"predecessor-version":[{"id":197059,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/175135\/revisions\/197059"}],"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=175135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=175135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=175135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}