<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div>Dear all, <br class=""><br class="">I am currently playing with a concept in Swift to store statistics in apps using generic types. One of the key parts are the ability to register properties (for example booleans, strings and integers - but also more complex data types such as dates):<br class=""><br class="">stats.add(description: "iOS Version", property: {<br class=""> &nbsp;&nbsp;&nbsp;return UIDevice.current.systemVersion as? String<br class="">}))<br class=""><br class="">stats.add(description: "HealthKit authorized", property: {<br class=""> &nbsp;&nbsp;&nbsp;return HealthKit.authorized<br class="">}))<br class=""><br class="">I've played with different kind of implementations of this, but feel that Swift limits me in some ways, because I can not find a way to store different types in a list.&nbsp;</div><div><br class=""></div><div>I have tried to implement it in Java, and this works:&nbsp;</div><div><br class=""></div><div><pre style="font-size: 9pt; background-color: rgb(255, 255, 255); font-family: Menlo;" class=""><span style="color: rgb(0, 0, 128); font-weight: bold;" class="">import </span>java.util.ArrayList;<br class=""><span style="color: rgb(0, 0, 128); font-weight: bold;" class="">import </span>java.util.List;</pre></div><div><pre style="background-color: rgb(255, 255, 255); font-family: Menlo; font-size: 9pt;" class=""><span style="color:#000080;font-weight:bold;" class="">public class </span>Property&lt;<span style="color:#20999d;" class="">T</span>&gt; {<br class="">    <span style="color:#000080;font-weight:bold;" class="">private </span>String <span style="color:#660e7a;font-weight:bold;" class="">description</span>;<br class="">    <span style="color:#000080;font-weight:bold;" class="">private </span><span style="color:#20999d;" class="">T </span><span style="color:#660e7a;font-weight:bold;" class="">property</span>;<br class=""><br class="">    <span style="color:#000080;font-weight:bold;" class="">public </span>Property(String description, <span style="color:#20999d;" class="">T </span>property){<br class="">        <span style="color:#000080;font-weight:bold;" class="">this</span>.<span style="color:#660e7a;font-weight:bold;" class="">description </span>= description;<br class="">        <span style="color:#000080;font-weight:bold;" class="">this</span>.<span style="color:#660e7a;font-weight:bold;" class="">property    </span>= property;<br class="">    }<br class=""><br class="">    <span style="color:#000080;font-weight:bold;" class="">public </span>String getDescription() {<br class="">        <span style="color:#000080;font-weight:bold;" class="">return </span><span style="color:#660e7a;font-weight:bold;" class="">description</span>;<br class="">    }<br class=""><br class="">    <span style="color:#000080;font-weight:bold;" class="">public </span><span style="color:#20999d;" class="">T </span>getProperty() {<br class="">        <span style="color:#000080;font-weight:bold;" class="">return </span><span style="color:#660e7a;font-weight:bold;" class="">property</span>;<br class="">    }<br class="">}</pre><pre style="background-color: rgb(255, 255, 255); font-family: Menlo; font-size: 9pt;" class=""><br class=""></pre><pre style="background-color: rgb(255, 255, 255); font-family: Menlo; font-size: 9pt;" class=""><pre style="font-family: Menlo; font-size: 9pt;" class=""><span style="color:#000080;font-weight:bold;" class="">public class </span>Analytics {<br class="">    <span style="color:#000080;font-weight:bold;" class="">private </span>List&lt;Property&gt; <span style="color:#660e7a;font-weight:bold;" class="">properties </span>= <span style="color:#000080;font-weight:bold;" class="">new </span>ArrayList&lt;&gt;();<br class=""><br class="">    <span style="color:#000080;font-weight:bold;" class="">public </span>Analytics() {<br class=""><br class="">    }<br class=""><br class="">    <span style="color:#000080;font-weight:bold;" class="">public static void </span>main(String[] args) {<br class="">        Analytics stats = <span style="color:#000080;font-weight:bold;" class="">new </span>Analytics();<br class="">        stats.add(<span style="color:#000080;font-weight:bold;" class="">new </span>Property(<span style="color:#008000;font-weight:bold;" class="">"HealthKit"</span>, <span style="color:#000080;font-weight:bold;" class="">false</span>));<br class="">        stats.add(<span style="color:#000080;font-weight:bold;" class="">new </span>Property(<span style="color:#008000;font-weight:bold;" class="">"iOS Version"</span>, <span style="color:#008000;font-weight:bold;" class="">"9.3"</span>));<br class="">        stats.show();<br class="">    }<br class=""><br class="">    <span style="color:#000080;font-weight:bold;" class="">public void </span>add(Property property) {<br class="">        <span style="color:#660e7a;font-weight:bold;" class="">properties</span>.add(property);<br class="">    }<br class=""><br class="">    <span style="color:#000080;font-weight:bold;" class="">public void </span>show() {<br class="">        <span style="color:#000080;font-weight:bold;" class="">for </span>(Property p : <span style="color:#660e7a;font-weight:bold;" class="">properties</span>) {<br class="">            System.<span style="color:#660e7a;font-weight:bold;font-style:italic;" class="">out</span>.println(p.getDescription() + <span style="color:#008000;font-weight:bold;" class="">": " </span>+ p.getProperty());<br class="">        }<br class="">    }<br class="">}</pre><pre style="font-family: Menlo; font-size: 9pt;" class=""><br class=""></pre><pre style="font-family: Menlo; font-size: 9pt;" class="">How would I be able to achieve this in Swift? My current idea of this approach does not seem to work, but also the idea to create specific subclasses for every type (and then type cast the different properties), gives a lot of work, and there would be a limitation for which types are available. </pre><pre style="font-family: Menlo; font-size: 9pt;" class=""><br class=""></pre><pre style="font-family: Menlo; font-size: 9pt;" class="">Thank you for your time. </pre><pre style="font-family: Menlo; font-size: 9pt;" class=""><br class=""></pre><pre style="font-family: Menlo; font-size: 9pt;" class="">Hugo Lundin. </pre></pre><br class=""><br class=""><br class=""></div></body></html>