<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"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></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,'Helvetica Neue',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,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',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,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',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)">"en_US"</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)">// "gregorian (fixed)"</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)">"calendar"</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 'Locale' has no member 'porpertyName'</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,'Helvetica Neue',Helvetica,sans-serif">In the last line of code above, the instance of Locale thinks I am passing it "propertyName" rather than the contents of the variable "calendar".</p><p style="margin:0px 0px 1em;padding:0px;border:0px;font-size:15px;clear:both;color:rgb(36,39,41);font-family:Arial,'Helvetica Neue',Helvetica,sans-serif">Is there any way to pass the value of propertyName ("calendar") to the instance of Locale? I know that in other languages, you can prepend the variable name like '$propertyName', 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,'Helvetica Neue',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,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',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,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',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'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 "propertyName", whereas currentLocale.propertyName.<wbr>contentsOf would pass the contents "calendar".</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'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>