{"id":536,"date":"2018-02-23T23:39:04","date_gmt":"2018-02-23T15:39:04","guid":{"rendered":"http:\/\/60.205.208.74\/2018\/02\/23\/swift-%e7%bb%93%e6%9e%84%e4%bd%93\/"},"modified":"2018-02-23T23:39:04","modified_gmt":"2018-02-23T15:39:04","slug":"swift-%e7%bb%93%e6%9e%84%e4%bd%93","status":"publish","type":"post","link":"https:\/\/www.puzi7.net\/?p=536","title":{"rendered":"Swift \u7ed3\u6784\u4f53"},"content":{"rendered":"<span itemprop=\"description\"><div class=\"article-body\">\n<div class=\"article-intro\" id=\"content\">\n<h1>Swift \u7ed3\u6784\u4f53<\/h1>\n<p>Swift \u7ed3\u6784\u4f53\u662f\u6784\u5efa\u4ee3\u7801\u6240\u7528\u7684\u4e00\u79cd\u901a\u7528\u4e14\u7075\u6d3b\u7684\u6784\u9020\u4f53\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4e3a\u7ed3\u6784\u4f53\u5b9a\u4e49\u5c5e\u6027\uff08\u5e38\u91cf\u3001\u53d8\u91cf\uff09\u548c\u6dfb\u52a0\u65b9\u6cd5\uff0c\u4ece\u800c\u6269\u5c55\u7ed3\u6784\u4f53\u7684\u529f\u80fd\u3002<\/p>\n<p>\u4e0e C \u548c Objective C \u4e0d\u540c\u7684\u662f\uff1a<\/p>\n<ul>\n<li>\n<p> \u7ed3\u6784\u4f53\u4e0d\u9700\u8981\u5305\u542b\u5b9e\u73b0\u6587\u4ef6\u548c\u63a5\u53e3\u3002 <\/p>\n<\/li>\n<li>\n<p>\t\u7ed3\u6784\u4f53\u5141\u8bb8\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u5355\u4e00\u6587\u4ef6\uff0c\u4e14\u7cfb\u7edf\u4f1a\u81ea\u52a8\u751f\u6210\u9762\u5411\u5176\u5b83\u4ee3\u7801\u7684\u5916\u90e8\u63a5\u53e3\u3002 <\/p>\n<\/li>\n<\/ul>\n<p>\u7ed3\u6784\u4f53\u603b\u662f\u901a\u8fc7\u88ab\u590d\u5236\u7684\u65b9\u5f0f\u5728\u4ee3\u7801\u4e2d\u4f20\u9012\uff0c\u56e0\u6b64\u5b83\u7684\u503c\u662f\u4e0d\u53ef\u4fee\u6539\u7684\u3002<\/p>\n<h3>\u8bed\u6cd5<\/h3>\n<p>\u6211\u4eec\u901a\u8fc7\u5173\u952e\u5b57 struct \u6765\u5b9a\u4e49\u7ed3\u6784\u4f53\uff1a<\/p>\n<pre>\nstruct nameStruct { \n   Definition 1\n   Definition 2\n   \u2026\u2026\n   Definition N\n}\n<\/pre>\n<hr>\n<h3>\u5b9e\u4f8b<\/h3>\n<p>\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3a MarkStruct \u7684\u7ed3\u6784\u4f53 \uff0c\u7ed3\u6784\u4f53\u7684\u5c5e\u6027\u4e3a\u5b66\u751f\u4e09\u4e2a\u79d1\u76ee\u7684\u5206\u6570\uff0c\u6570\u636e\u7c7b\u578b\u4e3a Int\uff1a<\/p>\n<pre>\nstruct MarkStruct{\n   var mark1: Int\n   var mark2: Int\n   var mark3: Int\n}\n<\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ed3\u6784\u4f53\u540d\u6765\u8bbf\u95ee\u7ed3\u6784\u4f53\u6210\u5458\u3002<\/p>\n<p>\u7ed3\u6784\u4f53\u5b9e\u4f8b\u5316\u4f7f\u7528 <b>let<\/b> \u5173\u952e\u5b57\uff1a<\/p>\n<pre>\nimport Cocoa\n\nstruct studentMarks {\n   var mark1 = 100\n   var mark2 = 78\n   var mark3 = 98\n}\nlet marks = studentMarks()\nprint(\"Mark1 \u662f \\(marks.mark1)\")\nprint(\"Mark2 \u662f \\(marks.mark2)\")\nprint(\"Mark3 \u662f \\(marks.mark3)\")\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\nMark1 \u662f 100\nMark2 \u662f 78\nMark3 \u662f 98\n<\/pre>\n<p>\u5b9e\u4f8b\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u7ed3\u6784\u4f53\u540d &#8216;studentMarks&#8217; \u8bbf\u95ee\u5b66\u751f\u7684\u6210\u7ee9\u3002\u7ed3\u6784\u4f53\u6210\u5458\u521d\u59cb\u5316\u4e3amark1, mark2, mark3\uff0c\u6570\u636e\u7c7b\u578b\u4e3a\u6574\u578b\u3002\n<\/p>\n<p>\n\u7136\u540e\u6211\u4eec\u901a\u8fc7\u4f7f\u7528 <b>let<\/b> \u5173\u952e\u5b57\u5c06\u7ed3\u6784\u4f53 studentMarks()  \u5b9e\u4f8b\u5316\u5e76\u4f20\u9012\u7ed9 marks\u3002<\/p>\n<p>\u6700\u540e\u6211\u4eec\u5c31\u901a\u8fc7 <b>.<\/b> \u53f7\u6765\u8bbf\u95ee\u7ed3\u6784\u4f53\u6210\u5458\u7684\u503c\u3002<\/p>\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u5316\u901a\u8fc7\u7ed3\u6784\u4f53\u5b9e\u4f8b\u5316\u65f6\u4f20\u503c\u5e76\u514b\u9686\u4e00\u4e2a\u7ed3\u6784\u4f53\uff1a<\/p>\n<pre>\nimport Cocoa\n\nstruct MarksStruct {\n   var mark: Int\n\n   init(mark: Int) {\n      self.mark = mark\n   }\n}\nvar aStruct = MarksStruct(mark: 98)\nvar bStruct = aStruct \/\/ aStruct \u548c bStruct \u662f\u4f7f\u7528\u76f8\u540c\u503c\u7684\u7ed3\u6784\u4f53\uff01\nbStruct.mark = 97\nprint(aStruct.mark) \/\/ 98\nprint(bStruct.mark) \/\/ 97\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n98\n97\n<\/pre>\n<hr>\n<h2>\u7ed3\u6784\u4f53\u5e94\u7528<\/h2>\n<p>\n\u5728\u4f60\u7684\u4ee3\u7801\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u7ed3\u6784\u4f53\u6765\u5b9a\u4e49\u4f60\u7684\u81ea\u5b9a\u4e49\u6570\u636e\u7c7b\u578b\u3002\n<\/p>\n<p>\n\u7ed3\u6784\u4f53\u5b9e\u4f8b\u603b\u662f\u901a\u8fc7\u503c\u4f20\u9012\u6765\u5b9a\u4e49\u4f60\u7684\u81ea\u5b9a\u4e49\u6570\u636e\u7c7b\u578b\u3002\n<\/p>\n<p>\u6309\u7167\u901a\u7528\u7684\u51c6\u5219\uff0c\u5f53\u7b26\u5408\u4e00\u6761\u6216\u591a\u6761\u4ee5\u4e0b\u6761\u4ef6\u65f6\uff0c\u8bf7\u8003\u8651\u6784\u5efa\u7ed3\u6784\u4f53\uff1a<\/p>\n<ul>\n<li>\u7ed3\u6784\u4f53\u7684\u4e3b\u8981\u76ee\u7684\u662f\u7528\u6765\u5c01\u88c5\u5c11\u91cf\u76f8\u5173\u7b80\u5355\u6570\u636e\u503c\u3002<\/li>\n<li>\u6709\u7406\u7531\u9884\u8ba1\u4e00\u4e2a\u7ed3\u6784\u4f53\u5b9e\u4f8b\u5728\u8d4b\u503c\u6216\u4f20\u9012\u65f6\uff0c\u5c01\u88c5\u7684\u6570\u636e\u5c06\u4f1a\u88ab\u62f7\u8d1d\u800c\u4e0d\u662f\u88ab\u5f15\u7528\u3002<\/li>\n<li>\u4efb\u4f55\u5728\u7ed3\u6784\u4f53\u4e2d\u50a8\u5b58\u7684\u503c\u7c7b\u578b\u5c5e\u6027\uff0c\u4e5f\u5c06\u4f1a\u88ab\u62f7\u8d1d\uff0c\u800c\u4e0d\u662f\u88ab\u5f15\u7528\u3002<\/li>\n<li>\u7ed3\u6784\u4f53\u4e0d\u9700\u8981\u53bb\u7ee7\u627f\u53e6\u4e00\u4e2a\u5df2\u5b58\u5728\u7c7b\u578b\u7684\u5c5e\u6027\u6216\u8005\u884c\u4e3a\u3002<\/li>\n<\/ul>\n<p>\u4e3e\u4f8b\u6765\u8bf4\uff0c\u4ee5\u4e0b\u60c5\u5883\u4e2d\u9002\u5408\u4f7f\u7528\u7ed3\u6784\u4f53\uff1a<\/p>\n<ul>\n<li>\u51e0\u4f55\u5f62\u72b6\u7684\u5927\u5c0f\uff0c\u5c01\u88c5\u4e00\u4e2a<code>width<\/code>\u5c5e\u6027\u548c<code>height<\/code>\u5c5e\u6027\uff0c\u4e24\u8005\u5747\u4e3a<code>Double<\/code>\u7c7b\u578b\u3002<\/li>\n<li>\u4e00\u5b9a\u8303\u56f4\u5185\u7684\u8def\u5f84\uff0c\u5c01\u88c5\u4e00\u4e2a<code>start<\/code>\u5c5e\u6027\u548c<code>length<\/code>\u5c5e\u6027\uff0c\u4e24\u8005\u5747\u4e3a<code>Int<\/code>\u7c7b\u578b\u3002<\/li>\n<li>\u4e09\u7ef4\u5750\u6807\u7cfb\u5185\u4e00\u70b9\uff0c\u5c01\u88c5<code>x<\/code>\uff0c<code>y<\/code>\u548c<code>z<\/code>\u5c5e\u6027\uff0c\u4e09\u8005\u5747\u4e3a<code>Double<\/code>\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p>\u7ed3\u6784\u4f53\u5b9e\u4f8b\u662f\u901a\u8fc7\u503c\u4f20\u9012\u800c\u4e0d\u662f\u901a\u8fc7\u5f15\u7528\u4f20\u9012\u3002<\/p>\n<pre>\nimport Cocoa\n\nstruct markStruct{\n    var mark1: Int\n    var mark2: Int\n    var mark3: Int\n    \n    init(mark1: Int, mark2: Int, mark3: Int){\n        self.mark1 = mark1\n        self.mark2 = mark2\n        self.mark3 = mark3\n    }\n}\n\nprint(\"\u4f18\u5f02\u6210\u7ee9:\")\nvar marks = markStruct(mark1: 98, mark2: 96, mark3:100)\nprint(marks.mark1)\nprint(marks.mark2)\nprint(marks.mark3)\n\nprint(\"\u7cdf\u7cd5\u6210\u7ee9:\")\nvar fail = markStruct(mark1: 34, mark2: 42, mark3: 13)\nprint(fail.mark1)\nprint(fail.mark2)\nprint(fail.mark3)\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n\u4f18\u5f02\u6210\u7ee9:\n98\n96\n100\n\u7cdf\u7cd5\u6210\u7ee9:\n34\n42\n13\n<\/pre>\n<p>\u4ee5\u4e0a\u5b9e\u4f8b\u4e2d\u6211\u4eec\u5b9a\u4e49\u4e86\u7ed3\u6784\u4f53  markStruct\uff0c\u4e09\u4e2a\u6210\u5458\u5c5e\u6027\uff1amark1, mark2 \u548c mark3\u3002\u7ed3\u6784\u4f53\u5185\u4f7f\u7528\u6210\u5458\u5c5e\u6027\u4f7f\u7528 self \u5173\u952e\u5b57\u3002\n<\/p>\n<p>\u4ece\u5b9e\u4f8b\u4e2d\u6211\u4eec\u53ef\u4ee5\u5f88\u597d\u7684\u7406\u89e3\u5230\u7ed3\u6784\u4f53\u5b9e\u4f8b\u662f\u901a\u8fc7\u503c\u4f20\u9012\u7684\u3002<\/p>\n<\/p><\/div>\n<\/p><\/div>\n<span>","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\"><span itemprop=\"description\">Swift \u7ed3\u6784\u4f53 Swift \u7ed3\u6784\u4f53\u662f\u6784\u5efa\u4ee3\u7801\u6240\u7528\u7684\u4e00\u79cd\u901a\u7528\u4e14\u7075\u6d3b\u7684\u6784\u9020\u4f53\u3002 \u6211\u4eec\u53ef\u4ee5\u4e3a&hellip;<\/span><\/p>\n<p class=\"more-link-p btn-align-right\"><a class=\"green zoom-btn\" href=\"https:\/\/www.puzi7.net\/?p=536\">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-536","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\/536","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=536"}],"version-history":[{"count":0,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/536\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}