<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">Now I understand your point. But as Jens said, Swift is a static language, it won't interpret `property` as a variable after `.`(dot). So for Swift compiler, you just refer to a none-exist property.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">Zhaoxin</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Sep 7, 2016 at 12:39 PM, Michael Sheaver <span dir="ltr"><<a href="mailto:msheaver@me.com" target="_blank">msheaver@me.com</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">Hi Zhao,<div><br></div><div>Many thanks for your response, and I will give this a try. However, I think that I might have used a bad example for the bigger question I was trying to ask, and for that I am sorry. </div><div><br></div><div>The question that I am really trying to address here is: Is there a more Swift-y way to, when passing a parameter to a function, we can tell it whether:</div><div><span style="white-space:pre-wrap">        </span>a) we intend to pass in the literal string, or</div><div><span style="white-space:pre-wrap">        </span>b) we want to pass the contents of the named variable?</div><div><br></div><div>I know that in some languages, if you prepend the passed parameter with a '$', as in $propertyName, the receiving function knows to use the <u>contents</u> of the variable named propertyName (in this case "calendar") instead of the literal string "propertyName". </div><div><br></div><div>Can we easily do this in Swift? If not, why not? Can we propose a change request to implement either a computed property or a method on the Any() class that would allow us to tell a called function whether we are passing in a literal type or a variable that contains the data to be processed?</div><div><br></div><div>Maybe this would violate the type safety for which Swift is thankfully so strongly trying to preserve. I don't know, but I would at lest like to consider it, for there ARE good business cases for it.</div><div><br></div><div>Does this help to clarify the question. I am posing?</div><div><br></div><div>Shawn, let me put this into a Swift file and shoot it your way.</div><div><br></div><div>Many thanks!</div><div><span class="HOEnZb"><font color="#888888"><div>Michael</div></font></span><div><div class="h5"><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;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"><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)"><br></div></div></div></span></div></span></div></div><div><blockquote type="cite"><div>On Sep 6, 2016, at 11:21 PM, Zhao Xin <<a href="mailto:owenzx@gmail.com" target="_blank">owenzx@gmail.com</a>> wrote:</div><br><div><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:<wbr>NSLocale.Key(rawValue:<wbr>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.con<wbr>tentsOf</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.con<wbr>tentsOf 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="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><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" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>
</div></blockquote></div><br></div></div></div></div></blockquote></div><br></div>