<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="">Hi Zhao,<div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">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 class=""><span class="Apple-tab-span" style="white-space:pre">        </span>a) we intend to pass in the literal string, or</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>b) we want to pass the contents of the named variable?</div><div class=""><br class=""></div><div class="">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 class="">contents</u> of the variable named propertyName (in this case "calendar") instead of the literal string "propertyName". </div><div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">Does this help to clarify the question. I am posing?</div><div class=""><br class=""></div><div class="">Shawn, let me put this into a Swift file and shoot it your way.</div><div class=""><br class=""></div><div class="">Many thanks!</div><div class=""><div class="">Michael</div><div class="">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-style-span" style="border-collapse: separate; font-family: Helvetica; font-style: normal; font-variant-ligatures: normal; font-variant-position: normal; font-variant-caps: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; border-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-stroke-width: 0px;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><span class="Apple-style-span" style="border-collapse: separate; font-family: Helvetica; font-style: normal; font-variant-ligatures: normal; font-variant-position: normal; font-variant-caps: normal; font-variant-numeric: normal; font-variant-alternates: normal; font-variant-east-asian: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; border-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-stroke-width: 0px;"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0);" class=""><div style="margin: 0px; font-size: 20px; line-height: normal; font-family: Chalkduster; color: rgb(89, 67, 163);" class=""><br class=""></div></div></div></span></div></span></div></div><div><blockquote type="cite" class=""><div class="">On Sep 6, 2016, at 11:21 PM, Zhao Xin <<a href="mailto:owenzx@gmail.com" class="">owenzx@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><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 class=""></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 class=""></div><div class="gmail_default"><div class="gmail_default"><font face="georgia, serif" class="">let calendar2 = </font><span style="font-family:georgia,serif" class=""> (currentLocale as NSLocale).object(forKey:NSLocale.Key(rawValue:propertyName))</span></div><div class="gmail_default"><br class=""></div><div class="gmail_default"><font face="georgia, serif" class="">is just almost the same as `let calendar2 = calendar1`.</font></div><div class="gmail_default"><font face="georgia, serif" class=""><br class=""></font></div><div class="gmail_default"><font face="georgia, serif" class="">If you insist on using `NSLocale.Key`, you should use </font><span style="font-family:georgia,serif" class="">`NSLocale` instead of </span><font face="georgia, serif" class="">`Locale`.</font></div><div class="gmail_default"><font face="georgia, serif" class=""><br class=""></font></div><div class="gmail_default"><font face="georgia, serif" class=""><br class=""></font></div><div class="gmail_default"><font face="georgia, serif" class="">Zhaoxin</font></div></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Wed, Sep 7, 2016 at 10:35 AM, Michael Sheaver via swift-users <span dir="ltr" class=""><<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class=""><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" class="">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" class=""><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" class=""><span style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)" class="">let</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class=""> currentLocale </span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">=</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class=""> </span><span style="margin:0px;padding:0px;border:0px;color:rgb(43,145,175)" class="">Locale</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">(</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">identifier</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">:</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class=""> </span><span style="margin:0px;padding:0px;border:0px;color:rgb(125,39,39)" class="">"en_US"</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">)</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">
</span><span style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)" class="">let</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class=""> calendar1 </span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">=</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class=""> currentLocale</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">.</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">calendar </span><span style="margin:0px;padding:0px;border:0px;color:rgb(133,140,147)" class="">// "gregorian (fixed)"</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">
</span><span style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)" class="">let</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class=""> propertyName </span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">=</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class=""> </span><span style="margin:0px;padding:0px;border:0px;color:rgb(125,39,39)" class="">"calendar"</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">
</span><span style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)" class="">let</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class=""> calendar2 </span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">=</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class=""> currentLocale</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">.</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">propertyName </span><span style="margin:0px;padding:0px;border:0px;color:rgb(133,140,147)" class="">// 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" class="">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" class="">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" class="">I want to keep this pure Swift if possible.</p><div class=""><font color="#242729" face="Arial" class=""><span style="font-size:15px" class="">I posted this question on StackOverflow, and got the following that does work:</span></font></div></div><div class=""><br class=""></div><div class=""><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" class=""><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" class=""><span style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)" class="">let</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class=""> calendar2 </span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">=</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">
</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">(</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">currentLocale </span><span style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)" class="">as</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class=""> </span><span style="margin:0px;padding:0px;border:0px;color:rgb(43,145,175)" class="">NSLocale</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">).</span><span style="margin:0px;padding:0px;border:0px;color:rgb(16,16,148)" class="">object</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">(</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">forKey</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">:</span><span style="margin:0px;padding:0px;border:0px;color:rgb(43,145,175)" class="">NSLoca<wbr class="">le</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">.</span><span style="margin:0px;padding:0px;border:0px;color:rgb(43,145,175)" class="">Key</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">(</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">rawValue</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">:</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">propertyName</span><span style="margin:0px;padding:0px;border:0px;color:rgb(48,51,54)" class="">))</span></code></pre><div class=""><font color="#242729" face="Arial" class=""><span style="font-size:15px" class="">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 class=""><font color="#242729" face="Arial" class=""><span style="font-size:15px" class=""><br class=""></span></font></div><div class=""><font color="#242729" face="Arial" class=""><span style="font-size:15px" class=""> let calendar2 = currentLocale.propertyName.<wbr class="">contentsOf</span></font></div><div class=""><font color="#242729" face="Arial" class=""><span style="font-size:15px" class=""><br class=""></span></font></div><div class=""><font color="#242729" face="Arial" class=""><span style="font-size:15px" class="">or something similar. Thus currentLocale.propertyName would pass the literal "propertyName", whereas currentLocale.propertyName.<wbr class="">contentsOf would pass the contents "calendar".</span></font></div><div class=""><font color="#242729" face="Arial" class=""><span style="font-size:15px" class=""><br class=""></span></font></div><div class=""><font color="#242729" face="Arial" class=""><span style="font-size:15px" class="">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 class=""><font color="#242729" face="Arial" class=""><span style="font-size:15px" class=""><br class=""></span></font></div><div class=""><font color="#242729" face="Arial" class=""><span style="font-size:15px" class="">Sincerest Regards,</span></font></div><div class=""><font color="#242729" face="Arial" class=""><span style="font-size:15px" class="">Michael</span></font></div><br class=""><div class="">
<div style="letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; word-wrap: break-word;" class=""><span style="border-collapse:separate;line-height:normal;border-spacing:0px" class=""><div style="word-wrap:break-word" class=""><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" class=""><div style="word-wrap:break-word" class=""><div style="" class=""><div style="margin:0px;font-size:20px;line-height:normal;font-family:Chalkduster;color:rgb(89,67,163)" class=""><span class="">Michael Sheaver</span></div><div style="margin:0px;line-height:normal;font-family:Chalkduster;color:rgb(158,106,55)" class=""><span style="color:rgb(48,120,44)" class=""><a href="mailto:msheaver@me.com" target="_blank" class="">msheaver@me.com</a></span></div><div style="margin:0px;line-height:normal;font-family:Chalkduster;color:rgb(166,32,35)" class=""><br class=""></div></div></div></span></div></span></div></div></div><br class="">______________________________<wbr class="">_________________<br class="">
swift-users mailing list<br class="">
<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-users</a><br class="">
<br class=""></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></div></body></html>