{"id":269,"date":"2020-07-29T21:54:03","date_gmt":"2020-07-29T13:54:03","guid":{"rendered":"http:\/\/123.57.164.21\/?p=269"},"modified":"2020-08-07T20:26:33","modified_gmt":"2020-08-07T12:26:33","slug":"swift%e4%b8%ad%e7%9a%84class%e5%92%8cstruct","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=269","title":{"rendered":"Swift\u4e2d\u7684Class\u548cStruct"},"content":{"rendered":"\n<pre class=\"wp-block-preformatted\">\u533a\u522b1:\nClass\u662f\u5f15\u7528\u7c7b\u578b\uff0c  \u5f53\u503c\u4f20\u9012\u7684\u65f6\u5019\uff0c  \u5b83\u662f\u4f20\u9012\u5bf9\u5df2\u6709instance\u7684\u5f15\u7528\u3002\nStruct\u662f\u503c\u7c7b\u578b\uff0c  \u5f53\u503c\u8fdb\u884c\u4f20\u9012\u7684\u65f6\u5019\uff0c\u5b83\u4f1acopy\u4f20\u9012\u7684\u503c\u3002\n<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import UIKit\n\nclass Animal {\n    var name: String?\n    var weight = 0.0\n}\n\nlet cat = Animal()\ncat.name = \"cat\"\ncat.weight = 10\n \nlet blackCat = cat\nblackCat.name = \"blackCat\"\n\nprint(\"cat's name: \\(cat.name!), cat's weight: \\(cat.weight)\")\n\n\/\/ \u63a7\u5236\u53f0\u8f93\u51fa\uff1a \"cat's name: blackCat, cat's weight: 10.0\\n\"\n\/\/ \u53ef\u4ee5\u53d1\u73b0cat\u7684name\u4e5f\u53d8\u4e86<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import UIKit\n\nstruct Animal {\n    var name: String?\n    var weight = 0.0\n}\n\nvar cat = Animal()\ncat.name = \"cat\"\ncat.weight = 10\n \nvar blackCat = cat\nblackCat.name = \"blackCat\"\n\nprint(\"cat's name: \\(cat.name!), cat's weight: \\(cat.weight)\")\n\n\/\/ \u63a7\u5236\u53f0\u8f93\u51fa\uff1a \"cat's name: cat, cat's weight: 10.0\\n\"\n\/\/ \u53ef\u4ee5\u53d1\u73b0cat\u7684name\u6ca1\u6709\u53d8<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\u533a\u522b2: \u5982\u679cstruct\u7684instance\u58f0\u660e\u4e3alet\uff0c  \u662f\u4e0d\u80fd\u6539\u53d8instance\u7684\u503c\u7684<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import UIKit\n\nstruct Animal {\n    var name: String?\n    var weight = 0.0\n}\n\nlet cat = Animal()\ncat.name = \"cat\"  => \u6b64\u5904\u51fa\u9519 Cannot assign to property: 'cat' is a 'let' constant\ncat.weight = 10\n <\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\u533a\u522b3:\nstruct\u5b58\u50a8\u5728stack\u4e2d\uff0c  class\u5b58\u50a8\u5728heap\u4e2d\uff0c  struct\u66f4\u5feb\u3002\n\u4f7f\u7528struct\u4e0d\u9700\u8981\u8003\u8651\u5185\u5b58\u6cc4\u6f0f\u548c\u591a\u7ebf\u7a0b\u8bfb\u5199\u7684\u95ee\u9898\uff0c  \u56e0\u4e3a\u5728\u4f20\u9012\u503c\u7684\u65f6\u5019\u5b83\u4f1a\u8fdb\u884c\u503c\u7684copy\u3002\nclass\u652f\u6301\u7ee7\u627f\uff0c  struct\u4e0d\u652f\u6301\u7ee7\u627f\u3002\nclass\u58f0\u660e\u7684\u65b9\u6cd5\u4fee\u6539\u5c5e\u6027\u4e0d\u9700\u8981<code>mutating<\/code>\u5173\u952e\u5b57\uff0c   struct\u9700\u8981<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct FPoint {\n    var x = 0.0\n    var y = 0.0\n    \/\/\u5f53\u5728struct\u4fee\u6539\u5c5e\u6027\u7684\u65f6\u5019\u9700\u8981\u4f7f\u7528mutating\n    mutating func addX(add: Double) {\n        self.x = self.x + add\n    }\n}\n \nlet p1 = FPoint()\nprint(\"p1's x : \\(p1.x), p1's y: \\(p1.y)\") \/\/ p1's x : 0.0, p1's y: 0.0\n<\/pre>\n\n\n\n<p>\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u5806\u5e94\u8be5\u7b97\u662f\u64cd\u4f5c\u7cfb\u7edf\u62bd\u8c61\u7684\u3001\u800c\u6808\u7531CPU\u76f4\u63a5\u63d0\u4f9b\u652f\u6301\uff08\u4f46\u64cd\u4f5c\u7cfb\u7edf\u53ef\u4ee5\u51b3\u5b9a\u5e94\u7528\u7a0b\u5e8f\u6808\u5927\u5c0f\u7684\u4e0a\u9650\uff09\u3002\u4ece\u786c\u4ef6\u4e0a\u8bf4\uff0c\u5806\u548c\u6808\u6700\u7ec8\u90fd\u662f\u5185\u5b58\u6761\u4e0a\u7684\u82e5\u5e72\u5b58\u50a8\u5355\u5143\uff0c\u6240\u4ee5\u5e76\u6ca1\u6709\u4ec0\u4e48\u4e0d\u540c\u3002\u4f46\uff0c\u7531\u4e8e\u5f88\u591aCPU\u5bf9\u538b\u6808\u3001\u51fa\u6808\u64cd\u4f5c\u6709\u786c\u4ef6\uff08\u6307\u4ee4\uff09\u4e0a\u7684\u652f\u6301\uff0c\u6240\u4ee5\u5728\u6808\u533a\u5206\u914d\/\u5f52\u8fd8\u5185\u5b58\u901f\u5ea6\u6781\u5feb\uff08\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u5806\u4e0a\u5206\u914d\u7b80\u76f4\u662f\u9f9f\u901f\uff09\uff1b\u5c24\u5176\u662f\u51fd\u6570\u5185\u90e8\u7684\u5c40\u90e8\u53d8\u91cf\uff0c\u53ef\u4ee5\u8f7b\u6613\u4e0e\u51fd\u6570\u8c03\u7528\/\u8fd4\u56de\u7ed1\u5b9a\uff0c\u56e0\u6b64\u51e0\u4e4e\u6240\u6709\u7f16\u8bd1\u578b\u8bed\u8a00\u90fd\u4f1a\u5728\u5229\u7528\u6808\u7ba1\u7406\u5c40\u90e8\u53d8\u91cf\uff08\u800c\u4e14\u4f1a\u4f18\u5148\u4f7f\u7528\u7a7a\u95f2\u7684\u5bc4\u5b58\u5668\uff0c\u6240\u4ee5\u51e0\u4e4e\u6240\u6709\u9ad8\u7ea7\u8bed\u8a00\u90fd\u662f\u8bbf\u95ee\u5c40\u90e8\u53d8\u91cf\u901f\u5ea6\u6700\u5feb\uff09\u3002\u4e0d\u4ec5\u5982\u6b64\u3002\u7531\u4e8e\u6808\u5185\u5b58\u5206\u914d\/\u56de\u6536\u7684\u7279\u6b8a\u673a\u5236\uff0c\u4f7f\u5f97\u540c\u4e00\u51fd\u6570\u5185\u90e8\u7684\u201c\u5c40\u90e8\u53d8\u91cf\u201d\u603b\u662f\u5206\u914d\u5728\u540c\u4e00\u6bb5\u8fde\u7eed\u5185\u5b58\u7a7a\u95f4\u4e0a\u7684\uff1b\u90a3\u4e48\u5f15\u7528\u8fd9\u4e9b\u5c40\u90e8\u53d8\u91cf\u5c31\u4e0d\u592a\u5bb9\u6613\u51fa\u73b0CPU\u7f13\u5b58\u5931\u6548\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u533a\u522b1: Class\u662f\u5f15\u7528\u7c7b\u578b\uff0c \u5f53\u503c\u4f20\u9012\u7684\u65f6\u5019\uff0c \u5b83\u662f\u4f20\u9012\u5bf9\u5df2\u6709instance\u7684\u5f15\u7528\u3002 Struct\u662f\u503c\u7c7b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/269"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=269"}],"version-history":[{"count":7,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/269\/revisions"}],"predecessor-version":[{"id":460,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/269\/revisions\/460"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}