<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" target="_blank">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>