{"id":4893,"date":"2024-05-14T15:46:45","date_gmt":"2024-05-14T06:46:45","guid":{"rendered":"https:\/\/redwing.moo.jp\/cocoa\/?p=4893"},"modified":"2024-05-14T15:49:36","modified_gmt":"2024-05-14T06:49:36","slug":"result%e5%9e%8b%e3%80%81%e3%82%a8%e3%83%a9%e3%83%bc%e3%83%8f%e3%83%b3%e3%83%89%e3%83%aa%e3%83%b3%e3%82%b0%e3%81%ae%e3%83%a1%e3%83%a2","status":"publish","type":"post","link":"https:\/\/redwing.moo.jp\/cocoa\/archives\/4893","title":{"rendered":"Result\u578b\u3001\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0\u306e\u30e1\u30e2"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: swift; title: ; notranslate\" title=\"\">\nenum DatabaseError: Error {\n    case entryNotFound\n    case duplicatedEntry\n    case unexpected(reason: String)\n}\n\nstruct User {\n    let id: Int\n    let name: String\n}\n\n\/\/ Result \u3092\u8fd4\u3059\u30d1\u30bf\u30fc\u30f3\nfunc findUser() -&gt; Result&lt;User, DatabaseError&gt; {\n    \/\/ \u30e6\u30fc\u30b6\u3092\u691c\u7d22\u3057\u305f\u304c\u3001\u898b\u3064\u304b\u3089\u306a\u304b\u3063\u305f\u3068\u4eee\u5b9a\u3059\u308b\n    return .failure(.entryNotFound)\n}\n\nlet result = findUser()\nswitch result {\ncase .success(_):\n    print(&quot;success&quot;)\ncase let .failure(error):\n    switch error {\n    case .entryNotFound:\n        print(&quot;entry not found&quot;)\n    case .duplicatedEntry:\n        print(&quot;duplicated entry&quot;)\n    case .unexpected(let reason):\n        print(&quot;unexpected: \\(reason)&quot;)\n    }\n}\n\n\/\/ throw \u3059\u308b\u30d1\u30bf\u30fc\u30f3\nfunc findUserWithThrowing() throws -&gt; User {\n    throw DatabaseError.unexpected(reason: &quot;\u4e0d\u9069\u5207\u306a\u30e6\u30fc\u30b6&quot;)\n\/\/    throw NSError(domain: &quot;error&quot;, code: -1, userInfo: nil)\n\/\/    return User(id: 1, name: &quot;test user&quot;)\n}\n\ndo {\n    let user = try findUserWithThrowing()\n} catch DatabaseError.unexpected(let reason) {\n    print(&quot;unexpected: \\(reason)&quot;)\n}\n\n<\/pre><\/div>\n\n\n<p><strong>\u53c2\u8003\uff1a<\/strong><br><a href=\"https:\/\/swift.codelly.dev\/guide\/\u30a8\u30e9\u30fc\u51e6\u7406\/#\u30a8\u30e9\u30fc\u51e6\u7406\u306e\u7a2e\u985e\">Swift \u30a8\u30e9\u30fc\u51e6\u7406\u306e\u7a2e\u985e<\/a><br><a href=\"https:\/\/zenn.dev\/dena\/articles\/eb89f97beecd38\">\u3010Swift 5.5\u3011\u8a73\u89e3 Result<\/a><\/p>\n<a href=\"https:\/\/twitter.com\/redwing1300?ref_src=twsrc%5Etfw\" class=\"twitter-follow-button\" data-size=\"large\" data-show-count=\"false\">Follow @redwing1300<\/a><script async src=\"https:\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content --><div id=\"redwi-2981025561\" class=\"redwi- redwi-entity-placement\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-9652476346722993\" crossorigin=\"anonymous\"><\/script><ins class=\"adsbygoogle\" style=\"display:block;\" data-ad-client=\"ca-pub-9652476346722993\" \ndata-ad-slot=\"\" \ndata-ad-format=\"auto\"><\/ins>\n<script> \n(adsbygoogle = window.adsbygoogle || []).push({}); \n<\/script>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>\u53c2\u8003\uff1aSwift \u30a8\u30e9\u30fc\u51e6\u7406\u306e\u7a2e\u985e\u3010Swift 5.5\u3011\u8a73\u89e3 Result Follow @redwing1300<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[116],"tags":[],"class_list":["post-4893","post","type-post","status-publish","format-standard","hentry","category-swift"],"_links":{"self":[{"href":"https:\/\/redwing.moo.jp\/cocoa\/wp-json\/wp\/v2\/posts\/4893","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/redwing.moo.jp\/cocoa\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/redwing.moo.jp\/cocoa\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/redwing.moo.jp\/cocoa\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/redwing.moo.jp\/cocoa\/wp-json\/wp\/v2\/comments?post=4893"}],"version-history":[{"count":4,"href":"https:\/\/redwing.moo.jp\/cocoa\/wp-json\/wp\/v2\/posts\/4893\/revisions"}],"predecessor-version":[{"id":4897,"href":"https:\/\/redwing.moo.jp\/cocoa\/wp-json\/wp\/v2\/posts\/4893\/revisions\/4897"}],"wp:attachment":[{"href":"https:\/\/redwing.moo.jp\/cocoa\/wp-json\/wp\/v2\/media?parent=4893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/redwing.moo.jp\/cocoa\/wp-json\/wp\/v2\/categories?post=4893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/redwing.moo.jp\/cocoa\/wp-json\/wp\/v2\/tags?post=4893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}