{"id":28447,"date":"2020-03-24T16:52:39","date_gmt":"2020-03-24T20:52:39","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=28447"},"modified":"2020-03-24T16:52:39","modified_gmt":"2020-03-24T20:52:39","slug":"making-swift-properties-overridable-only-in-debug-builds","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2020\/03\/24\/making-swift-properties-overridable-only-in-debug-builds\/","title":{"rendered":"Making Swift Properties Overridable Only in Debug Builds"},"content":{"rendered":"<p><a href=\"https:\/\/www.swiftbysundell.com\/tips\/making-properties-overridable-only-in-debug-builds\/\">John Sundell<\/a>:<\/p>\n<blockquote cite=\"https:\/\/www.swiftbysundell.com\/tips\/making-properties-overridable-only-in-debug-builds\/\">\n<p>Occasionally, we might want to override certain properties in order to facilitate testing, to be able to work on a new feature, or to debug a problem.<\/p>\n<p>[&#8230;]<\/p>\n<p>Here&rsquo;s a way to mitigate that problem, using Swift&rsquo;s new <em><a href=\"https:\/\/www.swiftbysundell.com\/articles\/property-wrappers-in-swift\">property wrappers<\/a><\/em> feature in combination with the <code>DEBUG<\/code> compiler flag. By creating a <code>DebugOverridable<\/code> property wrapper, we can enforce that the properties that we wish to override during testing and development are not actually overridden within any of our code that we&rsquo;re shipping to production[&#8230;]<\/p>\n<\/blockquote>\n<p>This is a neat trick, though unfortunately property wrappers don&rsquo;t work with <code>@NSManaged<\/code>. What I have been doing is having my tests use underscored versions of the properties. These are a declared in an extension, which is conditionally compiled only when the <code>TEST<\/code> flag is set.<\/p>\n<p>I&rsquo;d still like to see Swift&rsquo;s access controls reworked to make testing easier. <code>@testable import<\/code> doesn&rsquo;t really do the job because it only works for symbols that are already visible at the package level. So you can only use <code>private<\/code> for stuff that will never be used from tests.<\/p>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2019\/06\/25\/swift-property-wrappers\/\">Swift Property Wrappers<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>John Sundell: Occasionally, we might want to override certain properties in order to facilitate testing, to be able to work on a new feature, or to debug a problem. [&#8230;] Here&rsquo;s a way to mitigate that problem, using Swift&rsquo;s new property wrappers feature in combination with the DEBUG compiler flag. By creating a DebugOverridable property [&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-03-24T20:52:42Z","apple_news_api_id":"84de40bd-061b-4987-8be1-5e295fcb495e","apple_news_api_modified_at":"2020-03-24T20:52:42Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AhN5AvQYbSYeL4V4pX8tJXg","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":[109,31,1667,30,1666,71,901,268],"class_list":["post-28447","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-coredata","tag-ios","tag-ios-13","tag-mac","tag-macos-10-15","tag-programming","tag-swift-programming-language","tag-testing"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/28447","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=28447"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/28447\/revisions"}],"predecessor-version":[{"id":28448,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/28447\/revisions\/28448"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=28447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=28447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=28447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}