{"id":7388,"date":"2022-09-13T22:24:06","date_gmt":"2022-09-13T14:24:06","guid":{"rendered":"http:\/\/123.57.164.21\/?p=7388"},"modified":"2022-09-13T22:24:06","modified_gmt":"2022-09-13T14:24:06","slug":"swift-%e5%85%b3%e9%94%ae%e5%ad%97%ef%bc%88typealias%e3%80%81associatedtype%ef%bc%89","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=7388","title":{"rendered":"Swift \u5173\u952e\u5b57\uff08typealias\u3001associatedType\uff09"},"content":{"rendered":"\n<p><strong>typealias<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4f7f\u7528<code>typealias<\/code>\u4e3a\u5e38\u7528\u6570\u636e\u7c7b\u578b\u8d77\u4e00\u4e2a<code>\u522b\u540d<\/code>\uff0c\u4e00\u65b9\u9762\u66f4\u5bb9\u6613\u901a\u8fc7\u522b\u540d\u7406\u89e3\u8be5\u7c7b\u578b\u7684\u7528\u9014\uff0c\u53e6\u4e00\u65b9\u9762\u8fd8\u53ef\u4ee5\u51cf\u5c11\u65e5\u5e38\u5f00\u53d1\u7684\u4ee3\u7801\u91cf\u3002<\/p>\n\n\n\n<ul><li>\u7ed9\u5df2\u6709\u7c7b\u578b\u91cd\u65b0\u5b9a\u4e49\u540d\u79f0\uff0c\u65b9\u4fbf\u4ee3\u7801\u9605\u8bfb<\/li><li>\u5b9a\u4e49\u95ed\u5305\uff0c\u7c7b\u4f3coc\u7684block \u5b9a\u4e49<\/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=\"\">\n    \/\/eg:1.\u5df2\u6709\u7c7b\u578b\u7684\u91cd\u65b0\u547d\u540d\n    typealias  Address = CGPoint\n\n    let point: CGPoint = CGPoint(x: 0,y: 0)\n    \/\/\u7b49\u4ef7\u4e8e\n    let point: Address = CGPoint(x: 0,y: 0)<\/pre>\n\n\n\n<p>\u95ed\u5305\u5b9a\u4e49<\/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=\"\">    typealias successCallback = (_ code: String, _ message: String) -> Void\n    var callBack: successCallback?\n    self.callBack!(code: \"0\",message: \"ok\")\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=\"\">\/\/ \u7f51\u7edc\u8bf7\u6c42\u5e38\u7528\u56de\u8c03\u95ed\u5305\ntypealias success = (_ data: String) -> Void\ntypealias fail = (_ error: String) -> Void\n\nfunc fetchData(_ url: String, success: success, fail: fail) {\n}\n\n\/\/ \u7528&amp;\u8fde\u63a5\u591a\u4e2a\u534f\u8bae\ntypealias UITableViewCommonProtocol = UITableViewDelegate &amp; UITableViewDataSource\n\nclass TestDelegate:UITableViewCommonProtocol{\n    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {\n        return 0\n    }\n    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {\n        let cell = tableView.dequeueReusableCell(withIdentifier: \"cell\")\n        return cell\n    }\n}<\/pre>\n\n\n\n<p><strong>associatedType<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><code>associatedtypen<\/code>\u8868\u793a\u4f4d\u7f6e\u7684\u6570\u636e\u7c7b\u578b\uff0c\u53ea\u662f\u5148\u5b9a\u4e49\u4e00\u4e2a\u540d\u5b57\uff0c\u5177\u4f53\u8868\u793a\u7684\u7c7b\u578b\u8981\u5728<code>\u6700\u7ec8\u4f7f\u7528\u7684\u65f6\u5019\u8fdb\u884c\u8d4b\u503c<\/code>\u3002\u5728\u5b9a\u4e49\u534f\u8bae\u65f6\uff0c\u53ef\u4ee5\u7528associatedtype\u58f0\u660e\u4e00\u4e2a\u6216\u591a\u4e2a\u7c7b\u578b\u4f5c\u4e3a\u534f\u8bae\u5b9a\u4e49\u7684\u4e00\u90e8\u5206\u3002<\/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=\"\">protocol NetworkRequest {\n    associatedtype DataType\n    func didReceiveData(_ data: DataType)\n}\n\nclass ViewModel: NetworkRequest {\n    \n    typealias DataType = String\n    \n    func didReceiveData(_ data: DataType) {\n        \n    }\n    \n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>typealias \u4f7f\u7528typealias\u4e3a\u5e38\u7528\u6570\u636e\u7c7b\u578b\u8d77\u4e00\u4e2a\u522b\u540d\uff0c\u4e00\u65b9\u9762\u66f4\u5bb9\u6613\u901a\u8fc7\u522b\u540d\u7406\u89e3\u8be5\u7c7b\u578b\u7684\u7528\u9014\uff0c\u53e6\u4e00 [&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\/7388"}],"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=7388"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/7388\/revisions"}],"predecessor-version":[{"id":7389,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/7388\/revisions\/7389"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}