{"id":38750,"date":"2023-03-13T18:08:54","date_gmt":"2023-03-13T22:08:54","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=38750"},"modified":"2023-03-16T15:40:33","modified_gmt":"2023-03-16T19:40:33","slug":"pattern-matching-on-swift-error-codes","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2023\/03\/13\/pattern-matching-on-swift-error-codes\/","title":{"rendered":"Pattern Matching on Swift Error Codes"},"content":{"rendered":"<p><a href=\"https:\/\/oleb.net\/blog\/2023\/catch-error-code\/\">Ole Begemann<\/a>:<\/p>\n<blockquote cite=\"https:\/\/oleb.net\/blog\/2023\/catch-error-code\/\"><p>I was wondering why this shorter syntax works. Is there some special compiler magic for pattern matching against error codes of <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/nserror\"><code>NSError<\/code><\/a> instances? Turns out: no, the answer is much simpler. Foundation includes an overload for <a href=\"https:\/\/developer.apple.com\/documentation\/swift\/~=(_:_:)\">the pattern matching operator <code>~=<\/code><\/a> that matches error values against error codes.<\/p><p>The implementation looks something like this:<\/p><pre>public func ~= (code: CocoaError.Code, error: any Error) -&gt; Bool {\n    guard let error = error as? CocoaError else { return false }\n    return error.code == code\n}<\/pre><\/blockquote>\n<p>However, my experience has been that this only works for concrete types, not when your errors or codes <a href=\"https:\/\/github.com\/rileytestut\/AltStore\/blob\/cb4388d0a783972e86a98b3e0b6e93b4faccaea5\/Shared\/Errors\/ALTLocalizedError.swift#L169\">use a protocol<\/a>.<\/p>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2015\/04\/02\/custom-swift-switch-matchers\/\">Custom Swift Switch Matchers<\/a><\/li>\n<\/ul>\n\n<p id=\"pattern-matching-on-swift-error-codes-update-2023-03-15\">Update (2023-03-15): See also: <a href=\"https:\/\/twitter.com\/ConfusedVorlon\/status\/1635659389435535360\">Rob Jonson<\/a>.<\/p>\n\n<p id=\"pattern-matching-on-swift-error-codes-update-2023-03-16\">Update (2023-03-16): The problem <a href=\"https:\/\/forums.swift.org\/t\/how-does-switch-work-not-the-same-as-if-else-with-pattern-matching-operator\/63755\">seems to be<\/a> <a href=\"https:\/\/github.com\/apple\/swift\/issues\/56618\">this bug<\/a> (via <a href=\"https:\/\/twitter.com\/ConfusedVorlon\/status\/1636354077058637826\">Rob Jonson<\/a>).<\/p>","protected":false},"excerpt":{"rendered":"<p>Ole Begemann: I was wondering why this shorter syntax works. Is there some special compiler magic for pattern matching against error codes of NSError instances? Turns out: no, the answer is much simpler. Foundation includes an overload for the pattern matching operator ~= that matches error values against error codes.The implementation looks something like this:public [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2023-03-13T22:08:57Z","apple_news_api_id":"fc195837-19b7-4477-b2b2-f2be0748a3e1","apple_news_api_modified_at":"2023-03-16T19:40:36Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/A_BlYNxm3RHeysvK-B0ij4Q","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[69,31,2185,46,30,2223,857,71,901],"class_list":["post-38750","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-ios","tag-ios-16","tag-languagedesign","tag-mac","tag-macos-13-ventura","tag-nserror","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/38750","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=38750"}],"version-history":[{"count":3,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/38750\/revisions"}],"predecessor-version":[{"id":38791,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/38750\/revisions\/38791"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=38750"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=38750"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=38750"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}