{"id":562,"date":"2023-02-25T19:07:07","date_gmt":"2023-02-25T11:07:07","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=562"},"modified":"2023-04-29T20:35:42","modified_gmt":"2023-04-29T12:35:42","slug":"swift-class-initialization-and-deinitialization-methods-init-and-deinit","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/swift-class-initialization-and-deinitialization-methods-init-and-deinit\/","title":{"rendered":"Swift &#8211; \u7c7b\u521d\u59cb\u5316\u548c\u53cd\u521d\u59cb\u5316\u65b9\u6cd5\uff08init\u4e0edeinit\uff09"},"content":{"rendered":"<blockquote>\n<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4<\/p>\n<\/blockquote>\n<h3>init()\uff1a\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5\uff08\u6784\u9020\u65b9\u6cd5\uff09<\/h3>\n<p><!-- more --><\/p>\n<pre><code class=\"language-swift\">\/********************************\n\u4f7f\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\n********************************\/\nclass Student {\n    \/\/\u7c7b\u5c5e\u6027\n    var name:String = &quot;&quot;\n    var number:Int = 0\n}\nvar student = Student()\n\n\/********************************\n\u81ea\u5b9a\u4e49\u6784\u9020\u51fd\u6570\n********************************\/\nclass Person {\n    \/\/\u7c7b\u5c5e\u6027\n    var name:String\n    var age:Int\n\n    \/\/\u7c7b\u6784\u9020\u51fd\u6570\n    init(newName:String, newAge:Int) {\n        self.name = newName\n        self.age = newAge\n    }\n\n    \/\/\u6210\u5458\u51fd\u6570\uff08\u5b9e\u4f8b\u65b9\u6cd5\uff09\n    func say() -&gt; String{\n        return &quot;\u6211\u53eb\\(name)&quot;\n    }\n}\nvar p = Person(newName: &quot;Joe.Ye&quot;, newAge: 32)\nprint(p.say())<\/code><\/pre>\n<h3>deinit()\uff1a\u7c7b\u53cd\u521d\u59cb\u5316\u65b9\u6cd5\uff08\u6790\u6784\u65b9\u6cd5\uff09<\/h3>\n<pre><code class=\"language-swift\">class DBClass {\n    var conn:Connection? = Connection()\n    deinit{\n        \/\/\u53ef\u4ee5\u505a\u4e00\u4e9b\u6e05\u7406\u5de5\u4f5c\n        self.conn!.close()\n        self.conn = nil\n    }\n}\n\nvar db:DBClass? = DBClass()\ndb = nil  \/\/\u8bbe\u7f6enil\u540e\u5373\u53ef\u6267\u884cdeinit()\u65b9\u6cd5<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce8\uff1a\u4ee3\u7801\u5df2\u5347\u7ea7\u81f3Swift4 init()\uff1a\u7c7b\u7684\u521d\u59cb\u5316\u65b9\u6cd5\uff08\u6784\u9020\u65b9\u6cd5\uff09 \/****************** [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[171],"tags":[],"class_list":["post-562","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/562","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=562"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/562\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}