<html><body><div><meta charset="utf-8"><span style="color: #242729; font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; font-size: 15px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; line-height: 19.5px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; display: inline !important; float: none; background-color: #ffffff;" data-mce-style="color: #242729; font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; font-size: 15px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; line-height: 19.5px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; display: inline !important; float: none; background-color: #ffffff;">Since </span><code style="margin: 0px; padding: 1px 5px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: pre-wrap; color: #242729; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #eff0f1;" data-mce-style="margin: 0px; padding: 1px 5px; border: 0px; font-size: 13px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: pre-wrap; color: #242729; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #eff0f1;">Array</code><span style="color: #242729; font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; font-size: 15px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; line-height: 19.5px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; display: inline !important; float: none; background-color: #ffffff;" data-mce-style="color: #242729; font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; font-size: 15px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; line-height: 19.5px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; display: inline !important; float: none; background-color: #ffffff;"> is a generic struct, the compiler needs know which type to use as the generic parameter.</span></div><div><span style="color: #242729; font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; font-size: 15px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; line-height: 19.5px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; display: inline !important; float: none; background-color: #ffffff;" data-mce-style="color: #242729; font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif; font-size: 15px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; line-height: 19.5px; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; display: inline !important; float: none; background-color: #ffffff;"><br data-mce-bogus="1"></span></div><div>What I think is do something like this below</div><div><br></div><div><p class="p1"></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2;">let</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;"> array:</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #703daa;">Array</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;"><</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #703daa;">Int</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;">> = [</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #272ad8;">1</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;">,</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #272ad8;">2</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;">,</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #272ad8;">3</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;">,</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #272ad8;">4</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;">,</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #272ad8;">5</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;">]</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2;">let</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;"> dictionary:[</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #703daa;">String</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #703daa;">Any</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;">] = [</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #d12f1b;">"numbers"</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #4f8187;">array</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;">]</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" data-mce-style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;"></span><br></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2;">if</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2;">let</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;"> value = </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #4f8187;">dictionary</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;">[</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #d12f1b;">"numbers"</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;">] {</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2;">let</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;"> type = value.</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2;">dynamicType</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #3d1d81;">print</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;">(type)</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2;">if</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2;">let</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;"> numbers = value </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2;">as</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;">? </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #703daa;">Array</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;"><</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #703daa;">Int</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;">> {</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #3d1d81;">print</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;">(numbers + [</span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #272ad8;">99</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;">])</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;"> }</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2;">else</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2;">if</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2;">let</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;"> numbers = value </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2;">as</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;">? </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #703daa;">Array</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;"><</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #703daa;">String</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;">> {</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #3d1d81;">print</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;">(numbers + [</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #d12f1b;">"99"</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;">])</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;"> }</span></p><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;">}</span></p></p><p></p></div><div><br>On Aug 25, 2016, at 08:39 PM, Zhao Xin via swift-users <swift-users@swift.org> wrote:<br><br></div><div><blockquote type="cite"><div class="msg-quote"><div dir="ltr"><div class="gmail_default" style="font-family: georgia,serif;">Thanks, Jordan. Is there any other way to do that?</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 Thu, Aug 25, 2016 at 6:40 PM, Johan Segerfeldt <span dir="ltr"><<a href="mailto:Johan.Segerfeldt@fiwe.se" onclick="return false;">Johan.Segerfeldt@fiwe.se</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0 0 0 .8ex; border-left: 1px #ccc solid; padding-left: 1ex;">Zhaoxin,<br> <br> You are trying to cast to a type which is defined by a variable at runtime.<br> Casting with `as` is done at compile-time. The variable has no defined value at this time.<br> <br> The compiler cannot infer what the value of a variable is going to be.<br> It can only infer types by static analysis.<br> <br> <br> Regards<br> Johan<br> <span class=""><br> > Is that possible to do dynamic casting?<br> ><br> > My code:<br> ><br> > ><br> > > importFoundation<br> > ><br> > ><br> > ><br> > ><br> > ><br> > ><br> > ><br> > ><br> > > letarray = [1,2,3,4,5]<br> > ><br> > ><br> > ><br> </span>> > letdictionary:[String:Any] = ["numbers":array]<br> <span class="">> ><br> > ><br> > ><br> > ><br> > ><br> > ><br> > ><br> > ><br> > > ifletvalue =dictionary["numbers"] {<br> > ><br> > ><br> > ><br> > > lettype = type(of: value)<br> > ><br> > ><br> > ><br> > > print(type)// Array<Int><br> > ><br> > ><br> > ><br> </span>> > letnumbers = valueas!Array<Int>// [1, 2, 3, 4, 5]<br><div class="HOEnZb"><div class="h5">> ><br> > ><br> > ><br> > > //let numbers2 = value as! type // error: use of undeclared type 'type'<br> > ><br> > ><br> > ><br> > > }<br> > ><br> > ><br> > As you can see, the dynamic casting leads an error. Is there a way to do this? Thanks.<br> ><br> ><br> ><br> ><br> ><br> ><br> > Zhaoxin<br> ><br> ><br> ><br> ><br> ><br> ></div></div></blockquote></div><br></div><div class="_stretch"><span class="body-text-content">_______________________________________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" onclick="return false;">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users">https://lists.swift.org/mailman/listinfo/swift-users</a><br></span></div></div></blockquote></div></body></html>