<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">&lt;<a href="mailto:Johan.Segerfeldt@fiwe.se" target="_blank">Johan.Segerfeldt@fiwe.se</a>&gt;</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>
&gt; Is that possible to do dynamic casting?<br>
&gt;<br>
&gt; My code:<br>
&gt;<br>
&gt; &gt;<br>
&gt; &gt; importFoundation<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; letarray = [1,2,3,4,5]<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
</span>&gt; &gt; letdictionary:[String:Any] = [&quot;numbers&quot;:array]<br>
<span class="">&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; ifletvalue =dictionary[&quot;numbers&quot;] {<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; lettype = type(of: value)<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; print(type)// Array&lt;Int&gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
</span>&gt; &gt; letnumbers = valueas!Array&lt;Int&gt;// [1, 2, 3, 4, 5]<br>
<div class="HOEnZb"><div class="h5">&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; //let numbers2 = value as! type // error: use of undeclared type &#39;type&#39;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; &gt; }<br>
&gt; &gt;<br>
&gt; &gt;<br>
&gt; As you can see, the dynamic casting leads an error. Is there a way to do this? Thanks.<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; Zhaoxin<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; </div></div></blockquote></div><br></div>