{"id":6614,"date":"2024-11-12T08:27:22","date_gmt":"2024-11-12T00:27:22","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6614"},"modified":"2024-11-12T08:27:22","modified_gmt":"2024-11-12T00:27:22","slug":"swift-%e4%bb%80%e4%b9%88%e6%97%b6%e5%80%99%e7%94%a8-struct%e3%80%81%e4%bb%80%e4%b9%88%e6%97%b6%e5%80%99%e7%94%a8-class","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6614","title":{"rendered":"Swift \u4ec0\u4e48\u65f6\u5019\u7528 Struct\u3001\u4ec0\u4e48\u65f6\u5019\u7528 Class"},"content":{"rendered":"<p>\u5728 Swift \u4e2d\uff0c<code>Struct<\/code> \u548c <code>Class<\/code> \u90fd\u53ef\u4ee5\u7528\u6765\u5b9a\u4e49\u6570\u636e\u7ed3\u6784\uff0c\u4f46\u5b83\u4eec\u5728\u5185\u5b58\u7ba1\u7406\u3001\u7ee7\u627f\u3001\u548c\u8bed\u4e49\u4e0a\u6709\u4e00\u4e9b\u5173\u952e\u533a\u522b\u3002\u4e0b\u9762\u662f\u4e00\u4e9b\u5173\u4e8e\u4f55\u65f6\u9009\u62e9 <code>Struct<\/code> \u548c <code>Class<\/code> \u7684\u5efa\u8bae\uff1a<\/p>\n<h3>\u4f7f\u7528 Struct \u7684\u573a\u666f<\/h3>\n<p><code>Struct<\/code> \u662f\u503c\u7c7b\u578b\uff0c\u9002\u5408\u7528\u6765\u5b9a\u4e49\u4e0d\u53ef\u53d8\u7684\u6570\u636e\u7ed3\u6784\u3002\u4ee5\u4e0b\u60c5\u51b5\u901a\u5e38\u9009\u62e9 <code>Struct<\/code>\uff1a<\/p>\n<ol>\n<li>\n<p><strong>\u4e0d\u9700\u8981\u7ee7\u627f\u7684\u7b80\u5355\u6570\u636e\u7ed3\u6784<\/strong>\uff1a<code>Struct<\/code> \u4e0d\u652f\u6301\u7ee7\u627f\uff0c\u5982\u679c\u4e0d\u9700\u8981\u4ece\u7236\u7c7b\u7ee7\u627f\u6216\u6d3e\u751f\uff0c\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528 <code>Struct<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u636e\u4e0d\u53ef\u53d8\u6216\u53ea\u9700\u590d\u5236\u6570\u636e<\/strong>\uff1a<code>Struct<\/code> \u662f\u503c\u7c7b\u578b\uff0c\u6bcf\u6b21\u8d4b\u503c\u6216\u4f20\u9012\u65f6\u4f1a\u521b\u5efa\u526f\u672c\uff0c\u56e0\u6b64\u9002\u5408\u5728\u9700\u8981\u4e0d\u53ef\u53d8\u526f\u672c\u7684\u6570\u636e\u7ed3\u6784\u4e2d\u4f7f\u7528\uff0c\u6bd4\u5982\u51e0\u4f55\u5f62\u72b6\u3001\u5750\u6807\u70b9\u7b49\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6bcf\u4e2a\u5b9e\u4f8b\u4e0d\u4f1a\u5728\u5176\u4ed6\u5730\u65b9\u88ab\u610f\u5916\u4fee\u6539\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u7ebf\u7a0b\u5b89\u5168<\/strong>\uff1a\u7531\u4e8e <code>Struct<\/code> \u662f\u503c\u7c7b\u578b\uff0c\u590d\u5236\u4f20\u9012\u540e\u4e0d\u5171\u4eab\u540c\u4e00\u4e2a\u5185\u5b58\u533a\u57df\uff0c\u5929\u751f\u7ebf\u7a0b\u5b89\u5168\uff0c\u9002\u5408\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u4f7f\u7528\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9e\u73b0\u76f8\u7b49\u6027\u6bd4\u8f83<\/strong>\uff1a\u503c\u7c7b\u578b\u66f4\u9002\u5408\u9700\u8981\u5224\u65ad\u5185\u5bb9\u662f\u5426\u76f8\u7b49\u7684\u573a\u666f\u3002<code>Struct<\/code> \u81ea\u52a8\u751f\u6210\u7684 <code>==<\/code> \u8fd0\u7b97\u7b26\u8ba9\u76f8\u7b49\u6027\u6bd4\u8f83\u66f4\u52a0\u4fbf\u6377\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\u9700\u6c42<\/strong>\uff1a\u5bf9\u4e8e\u5c0f\u578b\u3001\u9891\u7e41\u521b\u5efa\u7684\u5bf9\u8c61\uff0c<code>Struct<\/code> \u7684\u6808\u5206\u914d\u6bd4 <code>Class<\/code> \u7684\u5806\u5206\u914d\u66f4\u9ad8\u6548\u3002<\/p>\n<\/li>\n<\/ol>\n<h4>\u793a\u4f8b<\/h4>\n<pre><code class=\"language-swift\">struct Point {\n    var x: Int\n    var y: Int\n}\n\nlet p1 = Point(x: 0, y: 0)\nvar p2 = p1\np2.x = 10\n\/\/ p1 \u7684 x \u4f9d\u7136\u662f 0\uff0c\u56e0\u4e3a p2 \u662f p1 \u7684\u526f\u672c<\/code><\/pre>\n<h3>\u4f7f\u7528 Class \u7684\u573a\u666f<\/h3>\n<p><code>Class<\/code> \u662f\u5f15\u7528\u7c7b\u578b\uff0c\u9002\u5408\u9700\u8981\u5171\u4eab\u6570\u636e\u3001\u7ee7\u627f\u548c\u591a\u6001\u7684\u573a\u666f\u3002\u4ee5\u4e0b\u60c5\u51b5\u901a\u5e38\u9009\u62e9 <code>Class<\/code>\uff1a<\/p>\n<ol>\n<li>\n<p><strong>\u9700\u8981\u7ee7\u627f\u548c\u591a\u6001<\/strong>\uff1a\u5982\u679c\u9700\u8981\u4f7f\u7528\u9762\u5411\u5bf9\u8c61\u7684\u7279\u6027\uff0c\u6bd4\u5982\u7ee7\u627f\u3001\u65b9\u6cd5\u91cd\u5199\u7b49\uff0c\u5c31\u9700\u8981\u4f7f\u7528 <code>Class<\/code>\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9700\u8981\u5171\u4eab\u6216\u53ef\u53d8\u7684\u72b6\u6001<\/strong>\uff1a<code>Class<\/code> \u662f\u5f15\u7528\u7c7b\u578b\uff0c\u53ef\u4ee5\u5728\u591a\u4e2a\u5730\u65b9\u5f15\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61\u5e76\u4fee\u6539\u5b83\u3002\u5982\u679c\u591a\u4e2a\u5b9e\u4f8b\u4e4b\u95f4\u9700\u8981\u5171\u4eab\u72b6\u6001\uff0c\u6216\u4e00\u4e2a\u5b9e\u4f8b\u7684\u4fee\u6539\u8981\u53cd\u6620\u5230\u6240\u6709\u5f15\u7528\u4e2d\uff0c<code>Class<\/code> \u662f\u5408\u9002\u7684\u9009\u62e9\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9700\u8981\u63a7\u5236\u8d44\u6e90\u7684\u751f\u547d\u5468\u671f<\/strong>\uff1a<code>Class<\/code> \u652f\u6301 <code>deinit<\/code> \u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u5bf9\u8c61\u9500\u6bc1\u65f6\u6267\u884c\u7279\u5b9a\u7684\u6e05\u7406\u64cd\u4f5c\uff0c\u6bd4\u5982\u5173\u95ed\u6587\u4ef6\u6216\u7f51\u7edc\u8fde\u63a5\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5bf9\u8c61\u8f83\u5927\u6216\u751f\u547d\u5468\u671f\u8f83\u957f<\/strong>\uff1a\u5f15\u7528\u7c7b\u578b\u5728\u5806\u4e0a\u5206\u914d\uff0c\u9002\u5408\u8f83\u5927\u7684\u5bf9\u8c61\uff0c\u907f\u514d\u6808\u4e0a\u9891\u7e41\u590d\u5236\u9020\u6210\u6027\u80fd\u5f00\u9500\u3002<\/p>\n<\/li>\n<\/ol>\n<h4>\u793a\u4f8b<\/h4>\n<pre><code class=\"language-swift\">class Person {\n    var name: String\n    init(name: String) {\n        self.name = name\n    }\n}\n\nlet person1 = Person(name: &quot;Alice&quot;)\nlet person2 = person1\nperson2.name = &quot;Bob&quot;\n\/\/ person1 \u7684 name \u4e5f\u4f1a\u53d8\u6210 &quot;Bob&quot;\uff0c\u56e0\u4e3a person1 \u548c person2 \u5f15\u7528\u7684\u662f\u540c\u4e00\u4e2a\u5b9e\u4f8b<\/code><\/pre>\n<h3>\u5c0f\u7ed3<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u7279\u6027<\/th>\n<th>Struct<\/th>\n<th>Class<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u7c7b\u578b<\/td>\n<td>\u503c\u7c7b\u578b<\/td>\n<td>\u5f15\u7528\u7c7b\u578b<\/td>\n<\/tr>\n<tr>\n<td>\u5185\u5b58\u5206\u914d<\/td>\n<td>\u6808\u5206\u914d\uff08\u5c0f\u578b\u7ed3\u6784\u6027\u80fd\u66f4\u4f73\uff09<\/td>\n<td>\u5806\u5206\u914d<\/td>\n<\/tr>\n<tr>\n<td>\u7ee7\u627f<\/td>\n<td>\u4e0d\u652f\u6301<\/td>\n<td>\u652f\u6301<\/td>\n<\/tr>\n<tr>\n<td>\u751f\u547d\u5468\u671f\u63a7\u5236<\/td>\n<td>\u65e0\u9700 <code>deinit<\/code><\/td>\n<td>\u652f\u6301 <code>deinit<\/code><\/td>\n<\/tr>\n<tr>\n<td>\u7ebf\u7a0b\u5b89\u5168<\/td>\n<td>\u81ea\u7136\u7ebf\u7a0b\u5b89\u5168<\/td>\n<td>\u9700\u5c0f\u5fc3\u5e76\u53d1\u95ee\u9898<\/td>\n<\/tr>\n<tr>\n<td>\u76f8\u7b49\u6027\u6bd4\u8f83<\/td>\n<td>\u9002\u5408\u9700\u8981\u503c\u6bd4\u8f83\u7684\u573a\u666f<\/td>\n<td>\u901a\u5e38\u6bd4\u8f83\u5f15\u7528\u5730\u5740<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5728 Swift \u4e2d\uff0c<code>Struct<\/code> \u5e38\u7528\u4e8e\u6a21\u578b\u3001\u89c6\u56fe\u7b49\u6570\u636e\u5bf9\u8c61\uff0c\u800c <code>Class<\/code> \u5219\u591a\u7528\u4e8e\u9700\u8981\u590d\u6742\u72b6\u6001\u7ba1\u7406\u548c\u5171\u4eab\u7684\u4e1a\u52a1\u903b\u8f91\u5bf9\u8c61\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Swift \u4e2d\uff0cStruct \u548c Class \u90fd\u53ef\u4ee5\u7528\u6765\u5b9a\u4e49\u6570\u636e\u7ed3\u6784\uff0c\u4f46\u5b83\u4eec\u5728\u5185\u5b58\u7ba1\u7406\u3001\u7ee7\u627f\u3001\u548c\u8bed\u4e49\u4e0a\u6709\u4e00&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6614\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[135],"tags":[],"class_list":["post-6614","post","type-post","status-publish","format-standard","hentry","category-135"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6614","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6614"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6614\/revisions"}],"predecessor-version":[{"id":6615,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6614\/revisions\/6615"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}