<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">I think you messed up with `Locale` and `NSLocale`.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">`Locale` is a struct in Swift 3 to replace the legacy `NSLocale`. The latter is a class, it has an inner `structure` called `NSLocale.Key`. For `Locale`, there is no `NSLocale.Key`. All there keys are instance properties in `Locale`. So in your specific case, </div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default"><div class="gmail_default"><font face="georgia, serif">let calendar2 = </font><span style="font-family:georgia,serif"> (currentLocale as NSLocale).object(forKey:NSLocale.Key(rawValue:propertyName))</span></div><div class="gmail_default"><br></div><div class="gmail_default"><font face="georgia, serif">is just almost the same as `let calendar2 = calendar1`.</font></div><div class="gmail_default"><font face="georgia, serif"><br></font></div><div class="gmail_default"><font face="georgia, serif">If you insist on using `NSLocale.Key`, you should use </font><span style="font-family:georgia,serif">`NSLocale` instead of </span><font face="georgia, serif">`Locale`.</font></div><div class="gmail_default"><font face="georgia, serif"><br></font></div><div class="gmail_default"><font face="georgia, serif"><br></font></div><div class="gmail_default"><font face="georgia, serif">Zhaoxin</font></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Sep 7, 2016 at 10:35 AM, Michael Sheaver via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:15px;clear:both;color:rgb(36,39,41);font-family:Arial,&#39;Helvetica Neue&#39;,Helvetica,sans-serif">I am trying to build a table of current locale properties in code, and have encountered issues with trying to pass the value of a variable to a function:</p><pre style="margin-top:0px;padding:5px;border:0px;font-size:13px;width:auto;max-height:600px;overflow:auto;font-family:Consolas,Menlo,Monaco,&#39;Lucida Console&#39;,&#39;Liberation Mono&#39;,&#39;DejaVu Sans Mono&#39;,&#39;Bitstream Vera Sans Mono&#39;,&#39;Courier New&#39;,monospace,sans-serif;background-color:rgb(239,240,241);color:rgb(57,51,24);word-wrap:normal"><code style="margin:0px;padding:0px;border:0px;font-family:Consolas,Menlo,Monaco,&#39;Lucida Console&#39;,&#39;Liberation Mono&#39;,&#39;DejaVu Sans Mono&#39;,&#39;Bitstream Vera Sans Mono&#39;,&#39;Courier New&#39;,monospace,sans-serif;white-space:inherit"><span style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">let</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> currentLocale </span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">=</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> </span><span style="margin:0px;padding:0px;border:0px;color:rgb(43,145,175)">Locale</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">(</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">identifier</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">:</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> </span><span style="margin:0px;padding:0px;border:0px;color:rgb(125,39,39)">&quot;en_US&quot;</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">)</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">

</span><span style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">let</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> calendar1 </span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">=</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> currentLocale</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">.</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">calendar      </span><span style="margin:0px;padding:0px;border:0px;color:rgb(133,140,147)">// &quot;gregorian (fixed)&quot;</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">

</span><span style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">let</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> propertyName </span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">=</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> </span><span style="margin:0px;padding:0px;border:0px;color:rgb(125,39,39)">&quot;calendar&quot;</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">
</span><span style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">let</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> calendar2 </span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">=</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> currentLocale</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">.</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">propertyName </span><span style="margin:0px;padding:0px;border:0px;color:rgb(133,140,147)">// Error: Value of type &#39;Locale&#39; has no member &#39;porpertyName&#39;</span></code></pre><p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:15px;clear:both;color:rgb(36,39,41);font-family:Arial,&#39;Helvetica Neue&#39;,Helvetica,sans-serif">In the last line of code above, the instance of Locale thinks I am passing it &quot;propertyName&quot; rather than the contents of the variable &quot;calendar&quot;.</p><p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:15px;clear:both;color:rgb(36,39,41);font-family:Arial,&#39;Helvetica Neue&#39;,Helvetica,sans-serif">Is there any way to pass the value of propertyName (&quot;calendar&quot;) to the instance of Locale? I know that in other languages, you can prepend the variable name like &#39;$propertyName&#39;, and that tells it to read the value of the variable.</p><p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:15px;clear:both;color:rgb(36,39,41);font-family:Arial,&#39;Helvetica Neue&#39;,Helvetica,sans-serif">I want to keep this pure Swift if possible.</p><div><font color="#242729" face="Arial"><span style="font-size:15px">I posted this question on StackOverflow, and got the following that does work:</span></font></div></div><div><br></div><div><pre style="margin-top:0px;padding:5px;border:0px;font-size:13px;width:auto;max-height:600px;overflow:auto;font-family:Consolas,Menlo,Monaco,&#39;Lucida Console&#39;,&#39;Liberation Mono&#39;,&#39;DejaVu Sans Mono&#39;,&#39;Bitstream Vera Sans Mono&#39;,&#39;Courier New&#39;,monospace,sans-serif;background-color:rgb(239,240,241);color:rgb(57,51,24);word-wrap:normal"><code style="margin:0px;padding:0px;border:0px;font-family:Consolas,Menlo,Monaco,&#39;Lucida Console&#39;,&#39;Liberation Mono&#39;,&#39;DejaVu Sans Mono&#39;,&#39;Bitstream Vera Sans Mono&#39;,&#39;Courier New&#39;,monospace,sans-serif;white-space:inherit"><span style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">let</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> calendar2 </span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">=</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> 
    </span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">(</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">currentLocale </span><span style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">as</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)"> </span><span style="margin:0px;padding:0px;border:0px;color:rgb(43,145,175)">NSLocale</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">).</span><span style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)">object</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">(</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">forKey</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">:</span><span style="margin:0px;padding:0px;border:0px;color:rgb(43,145,175)">NSLoca<wbr>le</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">.</span><span style="margin:0px;padding:0px;border:0px;color:rgb(43,145,175)">Key</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">(</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">rawValue</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">:</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">propertyName</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)">))</span></code></pre><div><font color="#242729" face="Arial"><span style="font-size:15px">It does seem odd to me that we must do some crazy Objective-C gymnastics to make it work. It  would seem logical to have a computed property on the Any type named, let&#39;s say, contentsOf that would return or pass the contents of the variable to the called function. For example, to use the original code sample above, we could use:</span></font></div></div><div><font color="#242729" face="Arial"><span style="font-size:15px"><br></span></font></div><div><font color="#242729" face="Arial"><span style="font-size:15px">    let calendar2 = currentLocale.propertyName.<wbr>contentsOf</span></font></div><div><font color="#242729" face="Arial"><span style="font-size:15px"><br></span></font></div><div><font color="#242729" face="Arial"><span style="font-size:15px">or something similar. Thus currentLocale.propertyName would pass the literal &quot;propertyName&quot;, whereas currentLocale.propertyName.<wbr>contentsOf would pass the contents &quot;calendar&quot;.</span></font></div><div><font color="#242729" face="Arial"><span style="font-size:15px"><br></span></font></div><div><font color="#242729" face="Arial"><span style="font-size:15px">Does anyone else agree that we need this functionality, or am I way out in left field on this? Or is this already possible and I haven&#39;t yet figured it out?</span></font></div><div><font color="#242729" face="Arial"><span style="font-size:15px"><br></span></font></div><div><font color="#242729" face="Arial"><span style="font-size:15px">Sincerest Regards,</span></font></div><div><font color="#242729" face="Arial"><span style="font-size:15px">Michael</span></font></div><br><div>
<div style="color:rgb(0,0,0);letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><span style="border-collapse:separate;line-height:normal;border-spacing:0px"><div style="word-wrap:break-word"><span style="border-collapse:separate;font-family:Helvetica;font-style:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;border-spacing:0px"><div style="word-wrap:break-word"><div style="color:rgb(0,0,0)"><div style="margin:0px;font-size:20px;line-height:normal;font-family:Chalkduster;color:rgb(89,67,163)"><span>Michael Sheaver</span></div><div style="margin:0px;line-height:normal;font-family:Chalkduster;color:rgb(158,106,55)"><span style="color:rgb(48,120,44)"><a href="mailto:msheaver@me.com" target="_blank">msheaver@me.com</a></span></div><div style="margin:0px;line-height:normal;font-family:Chalkduster;color:rgb(166,32,35)"><br></div></div></div></span></div></span></div></div></div><br>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>