{"id":3144,"date":"2021-06-08T13:39:43","date_gmt":"2021-06-08T05:39:43","guid":{"rendered":"http:\/\/123.57.164.21\/?p=3144"},"modified":"2021-06-08T13:39:43","modified_gmt":"2021-06-08T05:39:43","slug":"swift-%e5%b1%9e%e6%80%a7%e8%a7%82%e5%af%9f%e8%80%85%ef%bc%88willset%e4%b8%8edidset%ef%bc%89","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=3144","title":{"rendered":"Swift &#8211; \u5c5e\u6027\u89c2\u5bdf\u8005\uff08willSet\u4e0edidSet\uff09"},"content":{"rendered":"\n<p>\u5c5e\u6027\u89c2\u5bdf\u8005\uff0c\u7c7b\u4f3c\u4e8e\u89e6\u53d1\u5668\u3002\u7528\u6765\u76d1\u89c6\u5c5e\u6027\u7684\u9664\u521d\u59cb\u5316\u4e4b\u5916\u7684\u5c5e\u6027\u503c\u53d8\u5316\uff0c\u5f53\u5c5e\u6027\u503c\u53d1\u751f\u6539\u53d8\u65f6\u53ef\u4ee5\u5bf9\u6b64\u4f5c\u51fa\u54cd\u5e94\u3002\u6709\u5982\u4e0b\u7279\u70b9\uff1a<br>1\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u5728\u5c5e\u6027\u503c\u6539\u53d8\u540e\u89e6\u53d1didSet\uff0c\u4e5f\u53ef\u4ee5\u5728\u5c5e\u6027\u503c\u6539\u53d8\u524d\u89e6\u53d1willSet\u3002<br>2\uff0c\u7ed9\u5c5e\u6027\u6dfb\u52a0\u89c2\u5bdf\u8005\u5fc5\u987b\u8981\u58f0\u660e\u6e05\u695a\u5c5e\u6027\u7c7b\u578b\uff0c\u5426\u5219\u7f16\u8bd1\u5668\u62a5\u9519\u3002<br>3\uff0cwillSet\u53ef\u4ee5\u5e26\u4e00\u4e2anewName\u7684\u53c2\u6570\uff0c\u6ca1\u6709\u7684\u8bdd\uff0c\u8be5\u53c2\u6570\u9ed8\u8ba4\u547d\u540d\u4e3anewValue\u3002<br>4\uff0cdidSet\u53ef\u4ee5\u5e26\u4e00\u4e2aoldName\u7684\u53c2\u6570\uff0c\u8868\u793a\u65e7\u7684\u5c5e\u6027\uff0c\u4e0d\u5e26\u7684\u8bdd\u9ed8\u8ba4\u547d\u540d\u4e3aoldValue\u3002<br>5\uff0c\u5c5e\u6027\u521d\u59cb\u5316\u65f6\uff0cwillSet\u548cdidSet\u4e0d\u4f1a\u8c03\u7528\u3002\u53ea\u6709\u5728\u521d\u59cb\u5316\u4e0a\u4e0b\u6587\u4e4b\u5916\uff0c\u5f53\u8bbe\u7f6e\u5c5e\u6027\u503c\u65f6\u624d\u4f1a\u8c03\u7528\u3002<br>6\uff0c\u5373\u4f7f\u662f\u8bbe\u7f6e\u7684\u503c\u548c\u539f\u6765\u503c\u76f8\u540c\uff0cwillSet\u548cdidSet\u4e5f\u4f1a\u88ab\u8c03\u7528<br><\/p>\n\n\n\n<p><strong>\u793a\u4f8b\u5982\u4e0b\uff1a<\/strong><\/p>\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=\"\">class People\n{\n    \/\/\u666e\u901a\u5c5e\u6027\n    var firstName:String = \"\"\n    var lastName:String  = \"\"\n    var nickName:String  = \"\"\n     \n    \/\/\u8ba1\u7b97\u5c5e\u6027\n    var fullName:String\n    {\n        get\n        {\n            return nickName + \" \" + firstName + \" \" + lastName\n        }\n    }\n     \n    \/\/\u5e26\u5c5e\u6027\u76d1\u89c6\u5668\u7684\u666e\u901a\u5c5e\u6027\n    var age:Int = 0\n    {\n        \/\/\u6211\u4eec\u9700\u8981\u5728age\u5c5e\u6027\u53d8\u5316\u524d\u505a\u70b9\u4ec0\u4e48\n        willSet\n        {\n            print(\"Will set an new value \\(newValue) to age\")\n        }\n        \/\/\u6211\u4eec\u9700\u8981\u5728age\u5c5e\u6027\u53d1\u751f\u53d8\u5316\u540e\uff0c\u66f4\u65b0\u4e00\u4e0bnickName\u8fd9\u4e2a\u5c5e\u6027\n        didSet\n        {\n            print(\"age filed changed form \\(oldValue) to \\(age)\")\n            if age&lt;10\n            {\n                nickName = \"Little\"\n            }else\n            {\n                nickName = \"Big\"\n            }\n        }\n    }\n     \n    func toString() -> String\n    {\n        return \"Full Name: \\(fullName) \" + \", Age: \\(age) \"\n    }\n     \n}\n \nlet me = People()\nme.firstName = \"Li\"\nme.lastName  = \"Lei\"\nme.age = 30\n \nprint(me.toString())\n \n\/*\u7a0b\u5e8f\u8f93\u51fa\nWill set an new value 30 to age\nage filed changed form 0 to 30\nFull Name: Big Li Lei , Age: 30\n*\/<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5c5e\u6027\u89c2\u5bdf\u8005\uff0c\u7c7b\u4f3c\u4e8e\u89e6\u53d1\u5668\u3002\u7528\u6765\u76d1\u89c6\u5c5e\u6027\u7684\u9664\u521d\u59cb\u5316\u4e4b\u5916\u7684\u5c5e\u6027\u503c\u53d8\u5316\uff0c\u5f53\u5c5e\u6027\u503c\u53d1\u751f\u6539\u53d8\u65f6\u53ef\u4ee5\u5bf9\u6b64\u4f5c\u51fa\u54cd\u5e94\u3002\u6709\u5982\u4e0b\u7279 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,8],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3144"}],"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=3144"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3144\/revisions"}],"predecessor-version":[{"id":3145,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/3144\/revisions\/3145"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}