<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">Is that possible to do dynamic casting?</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">My code:</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div class="gmail_default" style="font-family:georgia,serif"><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">import</span><span style=""> Foundation</span></p></div><div class="gmail_default" style="font-family:georgia,serif"><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p></div><div class="gmail_default" style="font-family:georgia,serif"><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">let</span><span style=""> array = [</span><span style="color:rgb(39,42,216)">1</span><span style="">,</span><span style="color:rgb(39,42,216)">2</span><span style="">,</span><span style="color:rgb(39,42,216)">3</span><span style="">,</span><span style="color:rgb(39,42,216)">4</span><span style="">,</span><span style="color:rgb(39,42,216)">5</span><span style="">]</span></p></div><div class="gmail_default" style="font-family:georgia,serif"><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">let</span><span style=""> dictionary:[</span><span style="color:rgb(112,61,170)">String</span><span style="">:</span><span style="color:rgb(187,44,162)">Any</span><span style="">] = [</span><span style="color:rgb(209,47,27)">"numbers"</span><span style="">:</span><span style="color:rgb(79,129,135)">array</span><span style="">]</span></p></div><div class="gmail_default" style="font-family:georgia,serif"><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p></div><div class="gmail_default" style="font-family:georgia,serif"><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">if</span><span style=""> </span><span style="color:rgb(187,44,162)">let</span><span style=""> value = </span><span style="color:rgb(79,129,135)">dictionary</span><span style="">[</span><span style="color:rgb(209,47,27)">"numbers"</span><span style="">] {</span></p></div><div class="gmail_default" style="font-family:georgia,serif"><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> </span><span style="color:rgb(187,44,162)">let</span><span style=""> type = type(of: value)</span></p></div><div class="gmail_default" style="font-family:georgia,serif"><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(61,29,129)">print</span><span style="color:rgb(0,0,0)">(type) </span><span style="">// Array<Int></span></p></div><div class="gmail_default" style="font-family:georgia,serif"><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> </span><span style="color:rgb(187,44,162)">let</span><span style=""> numbers = value </span><span style="color:rgb(187,44,162)">as</span><span style="">! </span><span style="color:rgb(112,61,170)">Array</span><span style=""><</span><span style="color:rgb(112,61,170)">Int</span><span style="">> </span><span style="color:rgb(0,132,0)">// [1, 2, 3, 4, 5]</span></p></div><div class="gmail_default" style="font-family:georgia,serif"><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)"> </span><span style="">//let numbers2 = value as! type // error: use of undeclared type 'type'</span></p></div><div class="gmail_default" style="font-family:georgia,serif"><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="">}</span></p></div></blockquote><div class="gmail_default" style="font-family:georgia,serif">
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style=""></span></p>
</div><div class="gmail_default">
<p style="margin:0px;line-height:normal;min-height:13px"><font color="#000000" face="georgia, serif">As you can see, the dynamic casting leads an error. Is there a way to do this? Thanks.</font></p><p style="margin:0px;line-height:normal;min-height:13px"><font color="#000000" face="georgia, serif"><br></font></p><p style="margin:0px;line-height:normal;min-height:13px"><font color="#000000" face="georgia, serif">Zhaoxin</font></p>
</div></div>