<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">We were told that Bool can&#39;t downcast to AnyObject in Swift. However, with Foundation framework, we can.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default"><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="color:rgb(187,44,162)">do</span><span style=""> {</span></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="">    </span><span style="color:rgb(187,44,162)">let</span><span style=""> a:</span><span style="color:rgb(112,61,170)">Bool</span><span style=""> = </span><span style="color:rgb(187,44,162)">true</span></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="">    </span><span style="color:rgb(187,44,162)">let</span><span style=""> object:</span><span style="color:rgb(112,61,170)">AnyObject</span><span style=""> = a </span><span style="color:rgb(187,44,162)">as</span><span style=""> </span><span style="color:rgb(112,61,170)">AnyObject</span></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="">    object.</span><span style="color:rgb(187,44,162)">dynamicType</span></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">    </span><span style="">// __NSCFBoolean.Type</span></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="">    </span><span style="color:rgb(187,44,162)">let</span><span style=""> b:</span><span style="color:rgb(112,61,170)">Bool</span><span style=""> = object </span><span style="color:rgb(187,44,162)">as</span><span style="">! </span><span style="color:rgb(112,61,170)">Bool</span></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">    b </span><span style="">// true</span></p>
<p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="">}</span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style=""><br></span></p><p style="margin:0px;line-height:normal"><font face="georgia, serif">This feature works in some code automatically (like in a Dictionary), but in some other codes(like in a function), you have to downcast it yourself.</font></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><br></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="color:rgb(187,44,162)">do</span><span style=""> {</span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(112,61,170)"><span style="color:rgb(0,0,0)">    </span><span style="color:rgb(187,44,162)">var</span><span style="color:rgb(0,0,0)"> dictionary = </span><span style="">Dictionary</span><span style="color:rgb(0,0,0)">&lt;</span><span style="">String</span><span style="color:rgb(0,0,0)">, </span><span style="">AnyObject</span><span style="color:rgb(0,0,0)">&gt;()</span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span style="">    </span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="">    </span><span style="color:rgb(187,44,162)">func</span><span style=""> update&lt;T:</span><span style="color:rgb(112,61,170)">AnyObject</span><span style="">&gt;(value:</span><span style="color:rgb(79,129,135)">T</span><span style="">, key:</span><span style="color:rgb(112,61,170)">String</span><span style="">) {</span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="">        dictionary.</span><span style="color:rgb(61,29,129)">updateValue</span><span style="">(value, forKey: key)</span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="">    }</span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span style="">    </span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="">    </span><span style="color:rgb(187,44,162)">let</span><span style=""> aBool = </span><span style="color:rgb(187,44,162)">true</span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="">    </span><span style="color:rgb(187,44,162)">let</span><span style=""> key = </span><span style="color:rgb(209,47,27)">&quot;testBool&quot;</span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span style="">    </span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="">    dictionary.</span><span style="color:rgb(61,29,129)">updateValue</span><span style="">(aBool, forKey: key)</span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">    </span><span style="">// works</span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span style="">    </span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="">    </span><span style="color:rgb(49,89,93)">update</span><span style="">(aBool, key: key)</span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">    </span><span style="">// doesn&#39;t work. cannot invoke &#39;update&#39; with an argument list of type &#39;(Bool, key: String)&#39;</span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span style="">    </span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="">    </span><span style="color:rgb(49,89,93)">update</span><span style="">(aBool </span><span style="color:rgb(187,44,162)">as</span><span style=""> </span><span style="color:rgb(112,61,170)">AnyObject</span><span style="">, key:key)</span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)">    </span><span style="">// works</span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal">


















</p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="">}</span></p><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style=""><br></span></p><p style="margin:0px;line-height:normal"><font face="georgia, serif">My question: Is this normal? Should it all be automatic or not?</font></p><p style="margin:0px;line-height:normal"><span style=""><font face="georgia, serif"><br></font></span></p><p style="margin:0px;line-height:normal"><font face="georgia, serif">Zhaoxin</font></p></div></div>