<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"><channel><title>mikeash.com pyblog/friday-qa-2017-10-06-type-safe-user-defaults.html comments</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2017-10-06-type-safe-user-defaults.html#comments</link><description>mikeash.com Recent Comments</description><lastBuildDate>Sun, 12 Apr 2026 03:42:20 GMT</lastBuildDate><generator>PyRSS2Gen-1.0.0</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>rjt - 2017-10-07 21:32:19</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2017-10-06-type-safe-user-defaults.html#comments</link><description>@Macmade That's a beautifully elegant piece of code!</description><guid isPermaLink="true">72d65fed4b0d11d29212194ab3380128</guid><pubDate>Sat, 07 Oct 2017 21:32:19 GMT</pubDate></item><item><title>Macmade - 2017-10-06 17:34:46</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2017-10-06-type-safe-user-defaults.html#comments</link><description>Interesting implementation.
&lt;br /&gt;I usually use reflection in order to achieve this.
&lt;br /&gt;
&lt;br /&gt;Using the Objective-C runtime, or a Mirror in Swift.
&lt;br /&gt;
&lt;br /&gt;The goal is to declare usual properties, and let the reflection initialise and update them.
&lt;br /&gt;
&lt;br /&gt;Writing to NSUserDefaults is then done with KVO, for each reflected property, so everything is automatic, you just have to declare the properties.
&lt;br /&gt;
&lt;br /&gt;As an example: &lt;a href="https://gist.github.com/macmade/0824d91b1f3a3b095057a40742d50a03"&gt;https://gist.github.com/macmade/0824d91b1f3a3b095057a40742d50a03&lt;/a&gt;</description><guid isPermaLink="true">0495dc1722c538eb3270b94ba77906e6</guid><pubDate>Fri, 06 Oct 2017 17:34:46 GMT</pubDate></item><item><title>Maxim - 2017-10-06 16:59:25</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2017-10-06-type-safe-user-defaults.html#comments</link><description>I’ve been using SwiftyUserDefaults for a while:
&lt;br /&gt;&lt;a href="https://github.com/radex/SwiftyUserDefaults"&gt;https://github.com/radex/SwiftyUserDefaults&lt;/a&gt;
&lt;br /&gt;
&lt;br /&gt;Key differences:
&lt;br /&gt;- you have to declare both var name and key name (#2 in your wishlist)
&lt;br /&gt;- you can’t provide default value (#6). I made a pull request for that, but it’s ignored
&lt;br /&gt;&lt;a href="https://github.com/radex/SwiftyUserDefaults/pull/95"&gt;https://github.com/radex/SwiftyUserDefaults/pull/95&lt;/a&gt;
&lt;br /&gt;- I think it makes more semantic sense to subscript Defaults to get a value, instead of getting it from a key.value property. It’s clear that you get it from Defaults.</description><guid isPermaLink="true">0e16a0cdfb12b8153b6f87eab07420fc</guid><pubDate>Fri, 06 Oct 2017 16:59:25 GMT</pubDate></item><item><title>mikeash - 2017-10-06 15:27:53</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2017-10-06-type-safe-user-defaults.html#comments</link><description>Good call. I tried it with &lt;code&gt;Optional&amp;lt;Any&amp;gt;&lt;/code&gt; and that failed, but &lt;code&gt;Optional&amp;lt;Codable&amp;gt;&lt;/code&gt; appears to work.</description><guid isPermaLink="true">4779e9e93bdb6a63548745bd7512bed5</guid><pubDate>Fri, 06 Oct 2017 15:27:53 GMT</pubDate></item><item><title>Jean-Daniel - 2017-10-06 14:17:07</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2017-10-06-type-safe-user-defaults.html#comments</link><description>Instead of handling all types, what about handling only the Codable case ?
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;case let value as Optional&amp;lt;Codable&amp;gt; where value == nil: return nil&lt;/code&gt;
&lt;br /&gt;</description><guid isPermaLink="true">b685207f950a0eb283b801c7ba80eaa8</guid><pubDate>Fri, 06 Oct 2017 14:17:07 GMT</pubDate></item><item><title>Gwynne Raskind - 2017-10-06 13:52:11</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2017-10-06-type-safe-user-defaults.html#comments</link><description>Ooo, good point. I wonder if that's a limitation of Swift generics or just an algorithmic thing. Feels like the ability to do something similar to C++'s specializations would solve it.</description><guid isPermaLink="true">1c48ccd66e8cd6e40101cdad6e91f362</guid><pubDate>Fri, 06 Oct 2017 13:52:11 GMT</pubDate></item><item><title>mikeash - 2017-10-06 13:41:39</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2017-10-06-type-safe-user-defaults.html#comments</link><description>Man, you really can tell when I just write code directly in the article and don't compile it. Fixed, thanks.
&lt;br /&gt;
&lt;br /&gt;Your proposal for &lt;code&gt;nil&lt;/code&gt; checking doesn't work because you can't use the bare &lt;code&gt;Optional&lt;/code&gt; type. You have to specialize it. So you can do &lt;code&gt;as Optional&amp;lt;Int&amp;gt;&lt;/code&gt; or &lt;code&gt;as Optional&amp;lt;String&amp;gt;&lt;/code&gt; but you'd have to write out every type you wanted to handle.</description><guid isPermaLink="true">db1ebe3c29ef62bfa80996b4c25d4167</guid><pubDate>Fri, 06 Oct 2017 13:41:39 GMT</pubDate></item><item><title>Gwynne Raskind - 2017-10-06 13:35:58</title><link>http://www.mikeash.com/?page=pyblog/friday-qa-2017-10-06-type-safe-user-defaults.html#comments</link><description>In &lt;code&gt;let otherDefaults = UserDefaults(suitName: "...")!&lt;/code&gt; shouldn't that be &lt;code&gt;suiteName&lt;/code&gt;?
&lt;br /&gt;
&lt;br /&gt;Re: Optionals, why did you need the &lt;code&gt;isNil&lt;/code&gt; property versus treating it as an enum with something like &lt;code&gt;case let value as Optional where .none&lt;/code&gt; (except, you know, correct syntax that I don't remember because pattern matching syntax in Swift is weird)?</description><guid isPermaLink="true">4d5ec21e4a1801292cb95a8c5ae92007</guid><pubDate>Fri, 06 Oct 2017 13:35:58 GMT</pubDate></item></channel></rss>
