{"id":537,"date":"2018-02-23T23:39:05","date_gmt":"2018-02-23T15:39:05","guid":{"rendered":"http:\/\/60.205.208.74\/2018\/02\/23\/swift-%e7%b1%bb\/"},"modified":"2018-02-23T23:39:05","modified_gmt":"2018-02-23T15:39:05","slug":"swift-%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/www.puzi7.net\/?p=537","title":{"rendered":"Swift \u7c7b"},"content":{"rendered":"<span itemprop=\"description\"><div class=\"article-body\">\n<div class=\"article-intro\" id=\"content\">\n<h1>Swift \u7c7b<\/h1>\n<p>Swift \u7c7b\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\u7c7b\u5b9a\u4e49\u5c5e\u6027\uff08\u5e38\u91cf\u3001\u53d8\u91cf\uff09\u548c\u65b9\u6cd5\u3002<\/p>\n<p>\u4e0e\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\u6240\u4e0d\u540c\u7684\u662f\uff0cSwift \u5e76\u4e0d\u8981\u6c42\u4f60\u4e3a\u81ea\u5b9a\u4e49\u7c7b\u53bb\u521b\u5efa\u72ec\u7acb\u7684\u63a5\u53e3\u548c\u5b9e\u73b0\u6587\u4ef6\u3002\u4f60\u6240\u8981\u505a\u7684\u662f\u5728\u4e00\u4e2a\u5355\u4e00\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u7c7b\uff0c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u751f\u6210\u9762\u5411\u5176\u5b83\u4ee3\u7801\u7684\u5916\u90e8\u63a5\u53e3\u3002<\/p>\n<h2>\u7c7b\u548c\u7ed3\u6784\u4f53\u5bf9\u6bd4<\/h2>\n<p>Swift \u4e2d\u7c7b\u548c<a href=\"\/swift\/swift-structures.html\" target=\"_blank\">\u7ed3\u6784\u4f53<\/a>\u6709\u5f88\u591a\u5171\u540c\u70b9\u3002\u5171\u540c\u5904\u5728\u4e8e\uff1a<\/p>\n<ul>\n<li>\u5b9a\u4e49\u5c5e\u6027\u7528\u4e8e\u5b58\u50a8\u503c<\/li>\n<li>\u5b9a\u4e49\u65b9\u6cd5\u7528\u4e8e\u63d0\u4f9b\u529f\u80fd<\/li>\n<li>\u5b9a\u4e49\u9644\u5c5e\u811a\u672c\u7528\u4e8e\u8bbf\u95ee\u503c<\/li>\n<li>\u5b9a\u4e49\u6784\u9020\u5668\u7528\u4e8e\u751f\u6210\u521d\u59cb\u5316\u503c<\/li>\n<li>\u901a\u8fc7\u6269\u5c55\u4ee5\u589e\u52a0\u9ed8\u8ba4\u5b9e\u73b0\u7684\u529f\u80fd<\/li>\n<li>\u7b26\u5408\u534f\u8bae\u4ee5\u5bf9\u67d0\u7c7b\u63d0\u4f9b\u6807\u51c6\u529f\u80fd<\/li>\n<\/ul>\n<p>\u4e0e\u7ed3\u6784\u4f53\u76f8\u6bd4\uff0c\u7c7b\u8fd8\u6709\u5982\u4e0b\u7684\u9644\u52a0\u529f\u80fd\uff1a<\/p>\n<ul>\n<li>\u7ee7\u627f\u5141\u8bb8\u4e00\u4e2a\u7c7b\u7ee7\u627f\u53e6\u4e00\u4e2a\u7c7b\u7684\u7279\u5f81<\/li>\n<li>\u7c7b\u578b\u8f6c\u6362\u5141\u8bb8\u5728\u8fd0\u884c\u65f6\u68c0\u67e5\u548c\u89e3\u91ca\u4e00\u4e2a\u7c7b\u5b9e\u4f8b\u7684\u7c7b\u578b<\/li>\n<li>\u89e3\u6784\u5668\u5141\u8bb8\u4e00\u4e2a\u7c7b\u5b9e\u4f8b\u91ca\u653e\u4efb\u4f55\u5176\u6240\u88ab\u5206\u914d\u7684\u8d44\u6e90<\/li>\n<li>\u5f15\u7528\u8ba1\u6570\u5141\u8bb8\u5bf9\u4e00\u4e2a\u7c7b\u7684\u591a\u6b21\u5f15\u7528<\/li>\n<\/ul>\n<h3>\u8bed\u6cd5:<\/h3>\n<pre>\nclass classname {\n   Definition 1\n   Definition 2\n   \u2026\u2026\n   Definition N\n}\n<\/pre>\n<h3>\u7c7b\u5b9a\u4e49<\/h3>\n<pre>\nclass student{\n   var studname: String\n   var mark: Int \n   var mark2: Int \n}\n<\/pre>\n<p>\u5b9e\u4f8b\u5316\u7c7b\uff1a<\/p>\n<pre>let studrecord = student()<\/pre>\n<h3>\u5b9e\u4f8b<\/h3>\n<pre>\nimport Cocoa\n\nclass MarksStruct {\n    var mark: Int\n    init(mark: Int) {\n        self.mark = mark\n    }\n}\n\nclass studentMarks {\n    var mark = 300\n}\nlet marks = studentMarks()\nprint(\"\u6210\u7ee9\u4e3a \\(marks.mark)\")\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n\u6210\u7ee9\u4e3a 300\n<\/pre>\n<hr>\n<h2>\u4f5c\u4e3a\u5f15\u7528\u7c7b\u578b\u8bbf\u95ee\u7c7b\u5c5e\u6027<\/h2>\n<p>\n\u7c7b\u7684\u5c5e\u6027\u53ef\u4ee5\u901a\u8fc7 <b>.<\/b> \u6765\u8bbf\u95ee\u3002\u683c\u5f0f\u4e3a\uff1a<b>\u5b9e\u4f8b\u5316\u7c7b\u540d.\u5c5e\u6027\u540d<\/b>\uff1a<\/p>\n<pre>\nimport Cocoa\n\nclass MarksStruct {\n   var mark: Int\n   init(mark: Int) {\n      self.mark = mark\n   }\n}\n\nclass studentMarks {\n   var mark1 = 300\n   var mark2 = 400\n   var mark3 = 900\n}\nlet marks = studentMarks()\nprint(\"Mark1 is \\(marks.mark1)\")\nprint(\"Mark2 is \\(marks.mark2)\")\nprint(\"Mark3 is \\(marks.mark3)\")\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\nMark1 is 300\nMark2 is 400\nMark3 is 900\n<\/pre>\n<hr>\n<h2>\u6052\u7b49\u8fd0\u7b97\u7b26<\/h2>\n<p>\u56e0\u4e3a\u7c7b\u662f\u5f15\u7528\u7c7b\u578b\uff0c\u6709\u53ef\u80fd\u6709\u591a\u4e2a\u5e38\u91cf\u548c\u53d8\u91cf\u5728\u540e\u53f0\u540c\u65f6\u5f15\u7528\u67d0\u4e00\u4e2a\u7c7b\u5b9e\u4f8b\u3002<\/p>\n<p>\u4e3a\u4e86\u80fd\u591f\u5224\u5b9a\u4e24\u4e2a\u5e38\u91cf\u6216\u8005\u53d8\u91cf\u662f\u5426\u5f15\u7528\u540c\u4e00\u4e2a\u7c7b\u5b9e\u4f8b\uff0cSwift \u5185\u5efa\u4e86\u4e24\u4e2a\u6052\u7b49\u8fd0\u7b97\u7b26\uff1a<\/p>\n<table class=\"reference\">\n<tbody>\n<tr>\n<th><b>\u6052\u7b49\u8fd0\u7b97\u7b26<\/b><\/th>\n<th><b>\u4e0d\u6052\u7b49\u8fd0\u7b97\u7b26<\/b><\/th>\n<\/tr>\n<tr>\n<td>\u8fd0\u7b97\u7b26\u4e3a\uff1a===<\/td>\n<td>\u8fd0\u7b97\u7b26\u4e3a\uff1a!==<\/td>\n<\/tr>\n<tr>\n<td>\u5982\u679c\u4e24\u4e2a\u5e38\u91cf\u6216\u8005\u53d8\u91cf\u5f15\u7528\u540c\u4e00\u4e2a\u7c7b\u5b9e\u4f8b\u5219\u8fd4\u56de true<\/td>\n<td>\u5982\u679c\u4e24\u4e2a\u5e38\u91cf\u6216\u8005\u53d8\u91cf\u5f15\u7528\u4e0d\u540c\u4e00\u4e2a\u7c7b\u5b9e\u4f8b\u5219\u8fd4\u56de true<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u5b9e\u4f8b<\/h3>\n<pre>\nimport Cocoa\n\nclass SampleClass: Equatable {\n    let myProperty: String\n    init(s: String) {\n        myProperty = s\n    }\n}\nfunc ==(lhs: SampleClass, rhs: SampleClass) -&gt; Bool {\n    return lhs.myProperty == rhs.myProperty\n}\n\nlet spClass1 = SampleClass(s: \"Hello\")\nlet spClass2 = SampleClass(s: \"Hello\")\n\nif spClass1 === spClass2 {\/\/ false\n    print(\"\u5f15\u7528\u76f8\u540c\u7684\u7c7b\u5b9e\u4f8b \\(spClass1)\")\n}\n\nif spClass1 !== spClass2 {\/\/ true\n    print(\"\u5f15\u7528\u4e0d\u76f8\u540c\u7684\u7c7b\u5b9e\u4f8b \\(spClass2)\")\n}\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n\u5f15\u7528\u4e0d\u76f8\u540c\u7684\u7c7b\u5b9e\u4f8b SampleClass\n<\/pre>\n<\/p><\/div>\n<\/p><\/div>\n<span>","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\"><span itemprop=\"description\">Swift \u7c7b Swift \u7c7b\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\u7c7b\u5b9a\u4e49\u5c5e&hellip;<\/span><\/p>\n<p class=\"more-link-p btn-align-right\"><a class=\"green zoom-btn\" href=\"https:\/\/www.puzi7.net\/?p=537\">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-537","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\/537","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=537"}],"version-history":[{"count":0,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/537\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}