{"id":28040,"date":"2020-02-04T15:35:12","date_gmt":"2020-02-04T20:35:12","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=28040"},"modified":"2020-02-04T15:36:20","modified_gmt":"2020-02-04T20:36:20","slug":"nserrordomain-ns_error_enum-and-nserroruserinfokey","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2020\/02\/04\/nserrordomain-ns_error_enum-and-nserroruserinfokey\/","title":{"rendered":"NSErrorDomain, NS_ERROR_ENUM, and NSErrorUserInfoKey"},"content":{"rendered":"<p><a href=\"https:\/\/deezer.io\/whats-right-with-errors-2fe419424f62\">Bertrand Longevialle<\/a> (via <a href=\"https:\/\/twitter.com\/colincornaby\/status\/1182015792956620800\">Colin Cornaby<\/a>):<\/p>\n<blockquote cite=\"https:\/\/deezer.io\/whats-right-with-errors-2fe419424f62\"><p>Objective-C has a few macros to available to make your own <tt>NSError+YourDomain.[hm]<\/tt> self-documentingly crystal-clear. &#x1F44C;<\/p>\n<p>[&#8230;]<\/p>\n<p>Because it is imported in Swift as a specific type, properly defined <code>NSError<\/code> domains and code enums let the client benefit from all the completeness of <code>NSError<\/code> while writing first-class swift code.<\/p>\n<p>While <code>NSError<\/code> initing remains very similar, their handling gets shortened and clarified.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/stackoverflow.com\/a\/53542985\/6311\">Joachim Kurz<\/a>:<\/p>\n<blockquote cite=\"https:\/\/stackoverflow.com\/a\/53542985\/6311\">\n<p>What the Compiler does for the enum cases of an <code>NS_ERROR_ENUM<\/code> is two-fold:<\/p>\n<ul>\n<li>use the name of the enum and remove that prefix from all enum cases before importing them to swift<\/li>\n<li>create an enum with the given name to hold those cases. If the given name ends with <code>Code<\/code> remove that suffix.<\/li>\n<\/ul>\n<\/blockquote>\n\n<p>See also: <a href=\"https:\/\/developer.apple.com\/documentation\/swift\/cocoa_design_patterns\/handling_cocoa_errors_in_swift\">Handling Cocoa Errors in Swift<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Bertrand Longevialle (via Colin Cornaby): Objective-C has a few macros to available to make your own NSError+YourDomain.[hm] self-documentingly crystal-clear. &#x1F44C; [&#8230;] Because it is imported in Swift as a specific type, properly defined NSError domains and code enums let the client benefit from all the completeness of NSError while writing first-class swift code. While NSError [&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":"2020-02-04T20:35:15Z","apple_news_api_id":"c727a8c9-054e-4a6f-9cb3-77fa3ac1f92e","apple_news_api_modified_at":"2020-02-04T20:36:23Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/AxyeoyQVOSm-cs3f6OsH5Lg","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,1667,46,30,1666,857,54,71,901],"class_list":["post-28040","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-ios","tag-ios-13","tag-languagedesign","tag-mac","tag-macos-10-15","tag-nserror","tag-objective-c","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/28040","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=28040"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/28040\/revisions"}],"predecessor-version":[{"id":28047,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/28040\/revisions\/28047"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=28040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=28040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=28040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}