{"id":2815,"date":"2021-05-27T16:36:41","date_gmt":"2021-05-27T08:36:41","guid":{"rendered":"http:\/\/123.57.164.21\/?p=2815"},"modified":"2021-05-27T16:36:41","modified_gmt":"2021-05-27T08:36:41","slug":"swift-convenience-%e4%be%bf%e5%88%a9%e6%9e%84%e9%80%a0%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=2815","title":{"rendered":"Swift convenience \u4fbf\u5229\u6784\u9020\u51fd\u6570"},"content":{"rendered":"\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u7684\u6784\u9020\u65b9\u6cd5\u90fd\u662f\u6307\u5b9a\u6784\u9020\u51fd\u6570\u00a0<code>Designated<\/code><\/p>\n\n\n\n<p><code><strong>convenience<\/strong><\/code>\u00a0\u5173\u952e\u5b57\u4fee\u9970\u7684\u6784\u9020\u65b9\u6cd5\u5c31\u662f\u4fbf\u5229\u6784\u9020\u51fd\u6570<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u4fbf\u5229\u6784\u9020\u51fd\u6570\u5177\u6709\u4ee5\u4e0b\u7279\u70b9\uff1a<\/h5>\n\n\n\n<ul><li>\u53ef\u4ee5\u8fd4\u56de\u00a0<code>nil<\/code><\/li><li>\u53ea\u6709\u4fbf\u5229\u6784\u9020\u51fd\u6570\u4e2d\u53ef\u4ee5\u8c03\u7528\u00a0<code>self.<strong>init<\/strong>()<\/code><\/li><li>\u4fbf\u5229\u6784\u9020\u51fd\u6570\u4e0d\u80fd\u88ab <code>\u91cd\u5199<\/code> \u6216\u8005\u00a0<code>super<\/code><\/li><\/ul>\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=\"\">\/\/\/ `\u4fbf\u5229\u6784\u9020\u51fd\u6570`\n\/\/\/\n\/\/\/ - parameter name: \u59d3\u540d\n\/\/\/ - parameter age:  \u5e74\u9f84\n\/\/\/\n\/\/\/ - returns: Person \u5bf9\u8c61\uff0c\u5982\u679c\u5e74\u9f84\u8fc7\u5c0f\u6216\u8005\u8fc7\u5927\uff0c\u8fd4\u56de nil\nconvenience init?(name: String, age: Int) {\n    if age &lt; 20 || age > 100 {\n        return nil\n    }\n\n    self.init(dict: [\"name\": name, \"age\": age])\n}\n<\/pre>\n\n\n\n<p>\u6ce8\u610f\uff1a\u5728 Xcode \u4e2d\uff0c\u8f93\u5165 self.init \u65f6\u6ca1\u6709\u667a\u80fd\u63d0\u793a<\/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=\"\">\/\/\/ \u5b66\u751f\u7c7b\nclass Student: Person {\n\n    \/\/\/ \u5b66\u53f7\n    var no: String?\n\n    convenience init?(name: String, age: Int, no: String) {\n        self.init(name: name, age: age)\n\n        self.no = no\n    }\n}\n<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u4fbf\u5229\u6784\u9020\u51fd\u6570\u5e94\u7528\u573a\u666f:<\/h5>\n\n\n\n<ul><li>\u6839\u636e\u7ed9\u5b9a\u53c2\u6570\u5224\u65ad\u662f\u5426\u521b\u5efa\u5bf9\u8c61\uff0c\u800c\u4e0d\u50cf\u6307\u5b9a\u6784\u9020\u51fd\u6570\u90a3\u6837\u5fc5\u987b\u8981\u5b9e\u4f8b\u5316\u4e00\u4e2a\u5bf9\u8c61\u51fa\u6765<\/li><li>\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u53ef\u4ee5\u5bf9\u5df2\u6709\u7c7b\u7684\u6784\u9020\u51fd\u6570\u8fdb\u884c\u6269\u5c55\uff0c\u5229\u7528\u4fbf\u5229\u6784\u9020\u51fd\u6570\uff0c\u7b80\u5316\u5bf9\u8c61\u7684\u521b\u5efa<\/li><\/ul>\n\n\n\n<h5 class=\"wp-block-heading\">\u6784\u9020\u51fd\u6570\u5c0f\u7ed3<\/h5>\n\n\n\n<ul><li>\u6307\u5b9a\u6784\u9020\u51fd\u6570\u5fc5\u987b\u8c03\u7528\u5176\u76f4\u63a5\u7236\u7c7b\u7684\u7684\u6307\u5b9a\u6784\u9020\u51fd\u6570\uff08\u9664\u975e\u6ca1\u6709\u7236\u7c7b\uff09<\/li><li>\u4fbf\u5229\u6784\u9020\u51fd\u6570\u5fc5\u987b\u8c03\u7528\u540c\u4e00\u7c7b\u4e2d\u5b9a\u4e49\u7684\u5176\u4ed6<code>\u6307\u5b9a\u6784\u9020\u51fd\u6570<\/code>\u6216\u8005\u7528\u00a0<code>self.<\/code>\u00a0\u7684\u65b9\u5f0f\u8c03\u7528<code>\u7236\u7c7b\u7684\u4fbf\u5229\u6784\u9020\u51fd\u6570<\/code><\/li><li>\u4fbf\u5229\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u8fd4\u56de\u00a0<code>nil<\/code><\/li><li>\u4fbf\u5229\u6784\u9020\u51fd\u6570\u4e0d\u80fd\u88ab\u7ee7\u627f<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u7684\u6784\u9020\u65b9\u6cd5\u90fd\u662f\u6307\u5b9a\u6784\u9020\u51fd\u6570\u00a0Designated convenience\u00a0\u5173\u952e\u5b57\u4fee\u9970\u7684\u6784\u9020\u65b9\u6cd5 [&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\/2815"}],"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=2815"}],"version-history":[{"count":3,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/2815\/revisions"}],"predecessor-version":[{"id":2818,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/2815\/revisions\/2818"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}