{"id":839,"date":"2020-08-16T00:26:18","date_gmt":"2020-08-15T16:26:18","guid":{"rendered":"http:\/\/123.57.164.21\/?p=839"},"modified":"2020-08-16T00:27:49","modified_gmt":"2020-08-15T16:27:49","slug":"swiftui-view%e5%ae%9e%e7%8e%b0equatable%e5%8d%8f%e8%ae%ae","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=839","title":{"rendered":"SwiftUI View\u5b9e\u73b0Equatable\u534f\u8bae"},"content":{"rendered":"\n<p>\u672c\u7bc7\u6587\u7ae0\u8ba8\u8bba\u7684\u5185\u5bb9\u4e3b\u8981\u662fView\u7684\u5237\u65b0\u95ee\u9898\uff0c\u5f53view\u9700\u8981\u5237\u65b0\u65f6\uff0c\u5c31\u4f1a\u53bb\u8ba1\u7b97body\uff0c\u90a3\u4e48\uff0c\u5728\u67d0\u4e9b\u7279\u5b9a\u573a\u666f\u4e0b\uff0cview\u7684\u72b6\u6001\u5e76\u6ca1\u6709\u6539\u53d8\uff0c\u5982\u679c\u6bcf\u6b21\u90fd\u8ba1\u7b97body\uff0c\u80af\u5b9a\u4f1a\u9020\u6210\u6027\u80fd\u7684\u6d6a\u8d39\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c31\u63a2\u8ba8\u4e00\u4e0b\uff0c\u5982\u4f55\u8ba9view\u5b9e\u73b0Equatable\u534f\u8bae\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u6f14\u793a\u8fd9\u4e2a\u8fc7\u7a0b\uff0c\u6211\u4eec\u5199\u4e00\u4e2a\u5c0f\u7684demo\uff0c\u6bcf\u6b21\u70b9\u51fb\u6309\u94ae\uff0c\u968f\u673a\u751f\u6210\u4e00\u4e2a\u6574\u6570\uff0c\u6211\u4eec\u81ea\u5b9a\u4e49\u4e00\u4e2aview\uff0c\u663e\u793a\u8be5\u6570\u5b57\u662f\u5947\u6570\u8fd8\u662f\u5076\u6570\u3002<\/p>\n\n\n\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/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=\"\">struct ContentView: View {\n    @State private var n: Int = 0\n\n    var body: some View {\n        VStack {\n            Spacer()\n\n            OddOrEvenView(number: n)\n\n            Text(\"\\(n)\")\n                .foregroundColor(.primary)\n                .padding(.vertical, 20)\n\n            Button(\"\u968f\u673a\u751f\u6210\u6574\u6570\") {\n                self.n = Int.random(in: 10...1000)\n            }\n            .padding(.vertical, 20)\n\n            Spacer()\n        }\n    }\n}\n\nextension Int {\n    var isOdd: Bool { self % 2 != 0 }\n    var isEven: Bool { self % 2 == 0 }\n}\n\nstruct OddOrEvenView: View {\n    let number: Int\n\n    @State private var show = false\n\n    var body: some View {\n        print(\"OddOrEvenView\u5237\u65b0\u4e86\uff0c -- \\(self.number)\")\n        return VStack {\n            Text(\"\\(number.isOdd ? \"\u5947\u6570\" : \"\u5076\u6570\")\")\n                .font(.largeTitle)\n                .padding(20)\n                .foregroundColor(.white)\n                .background(RoundedRectangle(cornerRadius: 5).fill(number.isOdd ? Color.green : Color.blue))\n        }\n    }\n}<\/pre>\n\n\n\n<p>\u4e0a\u8fb9\u7684\u4ee3\u7801\u5f88\u7b80\u5355\uff0c\u8fd0\u884c\u540e\uff0c\u6548\u679c\u5982\u4e0b\u56fe\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"343\" height=\"330\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/08\/1914952-255a98c6201c506d-min-1.gif\" alt=\"\" class=\"wp-image-843\"\/><\/figure><\/div>\n\n\n\n<p>\u6253\u5370\u5982\u4e0b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"303\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/08\/image-93-1024x303.png\" alt=\"\" class=\"wp-image-844\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-93-1024x303.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-93-300x89.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-93-768x227.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-93-830x246.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-93-230x68.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-93-350x104.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-93-480x142.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-93.png 1304w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p>\u4ece\u4e0a\u8fb9\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u603b\u7ed3\u51fa\uff1a<\/p>\n\n\n\n<ul><li>\u5f53\u6211\u4eec\u4fee\u6539\u4e86<code>n<\/code>\u540e\uff0cOddOrEvenView\u90fd\u4f1a\u8ba1\u7b97body<\/li><li>\u5176\u5b9eOddOrEvenView\u6ca1\u5fc5\u8981\u6bcf\u6b21\u90fd\u8ba1\u7b97body\uff0c\u53ea\u6709\u5f53\u5947\u5076\u5207\u6362\u7684\u65f6\u5019\uff0c\u624d\u6709\u5fc5\u8981\u91cd\u65b0\u8ba1\u7b97body<\/li><\/ul>\n\n\n\n<p>\u6211\u4eec\u8ba9OddOrEvenView\u5b9e\u73b0Equatable\u534f\u8bae\uff1a<\/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=\"\">struct OddOrEvenView: View, Equatable {\n        ...\n    static func ==(lhs: OddOrEvenView, rhs: OddOrEvenView) -> Bool {\n        return lhs.number.isOdd == rhs.number.isOdd\n    }\n}\n<\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"343\" height=\"330\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/08\/1914952-f7b6c67863c72d0f-min.gif\" alt=\"\" class=\"wp-image-846\"\/><\/figure><\/div>\n\n\n\n<p>\u5728\u770b\u770b\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/08\/image-94-1024x265.png\" alt=\"\" class=\"wp-image-848\" width=\"580\" height=\"150\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-94-1024x265.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-94-300x78.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-94-768x199.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-94-830x215.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-94-230x60.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-94-350x91.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-94-480x124.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-94.png 1328w\" sizes=\"(max-width: 580px) 100vw, 580px\" \/><\/figure><\/div>\n\n\n\n<p>\u7ed3\u5408\u4e0a\u8fb9\u7684\u52a8\u56fe\u548c\u6253\u5370\u7ed3\u679c\uff0c\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0\uff0c\u5b9e\u73b0\u4e86Equatable\u540e\uff0c\u679c\u7136\u53ea\u6709\u5207\u6362\u5947\u5076\u624d\u4f1a\u91cd\u65b0\u8ba1\u7b97body\u3002<\/p>\n\n\n\n<p>\u6211\u5728\u4e0a\u8fb9\u7684\u4ee3\u7801\u4e2d\u57cb\u4e86\u4e00\u4e2a\u4f0f\u7b14\uff0c\u5927\u5bb6\u7559\u610f\u8fd9\u884c\u4ee3\u7801\u4e86\u6ca1\uff1f<\/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=\"\">@State private var show = false\n\n\n<\/pre>\n\n\n\n<p>\u5728OddOrEvenView\u4e2d\uff0c\u8fd9\u884c\u4ee3\u7801\u6ca1\u4ec0\u4e48\u5375\u7528\uff0c\u90a3\u6211\u4e3a\u4ec0\u4e48\u8fd8\u8981\u5199\u5b83\u5462\uff1f \u4e3a\u4e86\u8bf4\u660e\u8fd9\u91cc\u8fb9\u7684\u533a\u522b\uff0c\u54b1\u4eec\u5148\u53bb\u6389\u8fd9\u884c\u4ee3\u7801\uff0c\u7136\u540e\u8fd0\u884c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"343\" height=\"330\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/08\/1914952-c948f99fd3217b30.gif\" alt=\"\" class=\"wp-image-847\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"336\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/08\/image-95-1024x336.png\" alt=\"\" class=\"wp-image-849\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-95-1024x336.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-95-300x98.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-95-768x252.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-95-830x272.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-95-230x75.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-95-350x115.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-95-480x158.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-95.png 1310w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p>\u7ed3\u5408\u52a8\u56fe\u548c\u6253\u5370\u7ed3\u679c\uff0c\u6211\u4eec\u53d1\u73b0\uff0c\u5728\u6bcf\u6b21\u4fee\u6539<code>n<\/code>\u540e\uff0cOddOrEvenView\u6bcf\u6b21\u90fd\u8ba1\u7b97body\u4e86\u3002\u4e3a\u4ec0\u4e48\u5e76\u6ca1\u6709\u7528\u5230\u7684\u4ee3\u7801<code>@State private var show = false<\/code>\u5c31\u80fd\u8ba9Equatable\u751f\u6548\u5462\uff1f<\/p>\n\n\n\n<p><strong>\u5728\u5f53\u524d\u7248\u672c\u4e2d\u7684SwiftUI\u4e2d\uff0c\u5982\u679c\u67d0\u4e2aView\u5b9e\u73b0\u4e86Equatable\u534f\u8bae\uff0c\u53ea\u6709\u5f53\u5176\u5185\u90e8\u5b58\u5728\u590d\u6742\u5c5e\u6027\u7684\u60c5\u51b5\u4e0b\uff0c\u7cfb\u7edf\u624d\u4f1a\u8c03\u7528Equatable\u534f\u8bae\u4e2d\u7684\u51fd\u6570\u3002<\/strong><\/p>\n\n\n\n<p>\u90a3\u4e48\u4ec0\u4e48\u662f\u590d\u6742\u5c5e\u6027\u5462\uff1f \u7c7b\u4f3c\u4e8e\u7528<code>@State<\/code>, <code>ObservedObject<\/code>\u6216\u8005<code>environmentObject<\/code>\u7b49\u7b49\uff0c\u4e0e\u4e4b\u76f8\u5bf9\u5e94\u7684\u662fPOD(plain data), \u4e5f\u5c31\u662f\u57fa\u672c\u6570\u636e\u7c7b\u578b\u3002<\/p>\n\n\n\n<p>\u90a3\u4e48\u6709\u6ca1\u6709\u4e00\u79cd\u65b9\u6cd5\uff0c\u8ba9\u6211\u4eec\u4e0d\u7528\u5173\u5fc3View\u4e2d\u662f\u5426\u6709\u590d\u6742\u5c5e\u6027\uff0c\u4e5f\u80fd\u8c03\u7528Equatable\u534f\u8bae\u4e2d\u7684\u65b9\u6cd5\u5462\uff1f\u7b54\u6848\u5c31\u662f<code>EquatableView<\/code>\u6216<code>.equatable()<\/code>,\u4ed6\u4eec\u662f\u7b49\u6548\u7684\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=\"\">struct ContentView: View {\n    @State private var n: Int = 0\n\n    var body: some View {\n        VStack {\n            Spacer()\n\n            EquatableView(content: OddOrEvenView(number: n))\n\n            Text(\"\\(n)\")\n                .foregroundColor(.primary)\n                .padding(.vertical, 20)\n\n            Button(\"\u968f\u673a\u751f\u6210\u6574\u6570\") {\n                self.n = Int.random(in: 10...1000)\n            }\n            .padding(.vertical, 20)\n\n            Spacer()\n        }\n    }\n}\n<\/pre>\n\n\n\n<p>\u53ea\u9700\u8981\u4fee\u6539\u4e00\u70b9\u70b9\u4ee3\u7801\u5c31\u53ef\u4ee5\u4e86\uff0c\u6211\u5c31\u4e0d\u628a\u6253\u5370\u7ed3\u679c\u5f04\u4e0a\u6765\u4e86\uff0c\u5927\u5bb6\u6709\u5174\u8da3\u7684\u53ef\u4ee5\u81ea\u5df1\u8fd0\u884c\u5e76\u67e5\u770b\u6548\u679c\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=\"\">EquatableView(content: OddOrEvenView(number: n))\nOddOrEvenView(number: n).equatable()\n\n\n<\/pre>\n\n\n\n<p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u4e0a\u8fb9\u4e24\u79cd\u5199\u6cd5\u662f\u7b49\u6548\u7684\u3002\u4f46\u662f\uff0c\u7ecf\u8fc7\u6211\u7684\u9a8c\u8bc1\uff0c \u53ea\u6709View\u4e2d\u6709\u590d\u6742\u5c5e\u6027\u7684\u65f6\u5019\u624d\u4f1a\u8c03\u7528Equatable\u534f\u8bae\u4e2d\u7684\u51fd\u6570\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e5f\u7b97\u7b26\u5408\u903b\u8f91\uff0c\u6211\u505a\u4e00\u4e2a\u7b80\u5355\u7684\u603b\u7ed3\uff1a<\/p>\n\n\n\n<ul><li>\u5f53view\u4e2d\u5305\u542b\u590d\u6742\u5c5e\u6027\u7684\u65f6\u5019\uff0c\u5982\u679cview\u5b9e\u73b0\u4e86Equatable\u534f\u8bae\uff0c\u5219\u8c03\u7528\u6211\u4eec\u5199\u7684\u534f\u8bae\u51fd\u6570<\/li><\/ul>\n\n\n\n<ul><li>\u5f53view\u4e2d\u4e0d\u5305\u542b\u590d\u6742\u5c5e\u6027\u7684\u65f6\u5019\uff0c\u5982\u679c\u5b9e\u73b0\u4e86Equatable\u534f\u8bae\uff0c\u5219\u7cfb\u7edf\u4f1a\u81ea\u52a8\u6839\u636eview\u503c\u7684\u53d8\u5316\u8fdb\u884c\u5237\u65b0\uff0c\u5982\u679c\u6ca1\u6709\u5b9e\u73b0Equatable\u534f\u8bae\uff0c\u5219\u6bcf\u6b21\u90fd\u9700\u8981\u8ba1\u7b97body<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u7bc7\u6587\u7ae0\u8ba8\u8bba\u7684\u5185\u5bb9\u4e3b\u8981\u662fView\u7684\u5237\u65b0\u95ee\u9898\uff0c\u5f53view\u9700\u8981\u5237\u65b0\u65f6\uff0c\u5c31\u4f1a\u53bb\u8ba1\u7b97body\uff0c\u90a3\u4e48\uff0c\u5728\u67d0\u4e9b\u7279\u5b9a\u573a\u666f\u4e0b\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/839"}],"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=839"}],"version-history":[{"count":6,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/839\/revisions"}],"predecessor-version":[{"id":851,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/839\/revisions\/851"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}