<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On May 25, 2016, at 02:11, zh ao via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_default" style="font-family:georgia,serif">We were told that Bool can't downcast to AnyObject in Swift. However, with Foundation framework, we can.</div><div class="gmail_default" style="font-family:georgia,serif"><br class=""></div><div class="gmail_default"><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="color:rgb(187,44,162)" class="">do</span><span style="" class=""> {</span></div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="" class="">&nbsp; &nbsp; </span><span style="color:rgb(187,44,162)" class="">let</span><span style="" class=""> a:</span><span style="color:rgb(112,61,170)" class="">Bool</span><span style="" class=""> = </span><span style="color:rgb(187,44,162)" class="">true</span></div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="" class="">&nbsp; &nbsp; </span><span style="color:rgb(187,44,162)" class="">let</span><span style="" class=""> object:</span><span style="color:rgb(112,61,170)" class="">AnyObject</span><span style="" class=""> = a </span><span style="color:rgb(187,44,162)" class="">as</span><span style="" class=""> </span><span style="color:rgb(112,61,170)" class="">AnyObject</span></div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="" class="">&nbsp; &nbsp; object.</span><span style="color:rgb(187,44,162)" class="">dynamicType</span></div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="" class="">&nbsp; &nbsp; </span><span style="" class="">// __NSCFBoolean.Type</span></div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="" class="">&nbsp; &nbsp; </span><span style="color:rgb(187,44,162)" class="">let</span><span style="" class=""> b:</span><span style="color:rgb(112,61,170)" class="">Bool</span><span style="" class=""> = object </span><span style="color:rgb(187,44,162)" class="">as</span><span style="" class="">! </span><span style="color:rgb(112,61,170)" class="">Bool</span></div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="" class="">&nbsp; &nbsp; b </span><span style="" class="">// true</span></div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="" class="">}</span></div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="" class=""><br class=""></span></div><div style="margin: 0px; line-height: normal;" class=""><font face="georgia, serif" class="">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></div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><br class=""></div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="color:rgb(187,44,162)" class="">do</span><span style="" class=""> {</span></div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(112, 61, 170);" class=""><span style="" class="">&nbsp; &nbsp; </span><span style="color:rgb(187,44,162)" class="">var</span><span style="" class=""> dictionary = </span><span style="" class="">Dictionary</span><span style="" class="">&lt;</span><span style="" class="">String</span><span style="" class="">, </span><span style="" class="">AnyObject</span><span style="" class="">&gt;()</span></div><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px" class=""><span style="" class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></p><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="" class="">&nbsp; &nbsp; </span><span style="color:rgb(187,44,162)" class="">func</span><span style="" class=""> update&lt;T:</span><span style="color:rgb(112,61,170)" class="">AnyObject</span><span style="" class="">&gt;(value:</span><span style="color:rgb(79,129,135)" class="">T</span><span style="" class="">, key:</span><span style="color:rgb(112,61,170)" class="">String</span><span style="" class="">) {</span></div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="" class="">&nbsp; &nbsp; &nbsp; &nbsp; dictionary.</span><span style="color:rgb(61,29,129)" class="">updateValue</span><span style="" class="">(value, forKey: key)</span></div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="" class="">&nbsp; &nbsp; }</span></div><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px" class=""><span style="" class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></p><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="" class="">&nbsp; &nbsp; </span><span style="color:rgb(187,44,162)" class="">let</span><span style="" class=""> aBool = </span><span style="color:rgb(187,44,162)" class="">true</span></div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="" class="">&nbsp; &nbsp; </span><span style="color:rgb(187,44,162)" class="">let</span><span style="" class=""> key = </span><span style="color:rgb(209,47,27)" class="">"testBool"</span></div><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px" class=""><span style="" class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></p><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="" class="">&nbsp; &nbsp; dictionary.</span><span style="color:rgb(61,29,129)" class="">updateValue</span><span style="" class="">(aBool, forKey: key)</span></div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="" class="">&nbsp; &nbsp; </span><span style="" class="">// works</span></div><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px" class=""><span style="" class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></p><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="" class="">&nbsp; &nbsp; </span><span style="color:rgb(49,89,93)" class="">update</span><span style="" class="">(aBool, key: key)</span></div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="" class="">&nbsp; &nbsp; </span><span style="" class="">// doesn't work. cannot invoke 'update' with an argument list of type '(Bool, key: String)'</span></div><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px" class=""><span style="" class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></p><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="" class="">&nbsp; &nbsp; </span><span style="color:rgb(49,89,93)" class="">update</span><span style="" class="">(aBool </span><span style="color:rgb(187,44,162)" class="">as</span><span style="" class=""> </span><span style="color:rgb(112,61,170)" class="">AnyObject</span><span style="" class="">, key:key)</span></div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="" class="">&nbsp; &nbsp; </span><span style="" class="">// works</span></div><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal" class="">


















</p><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="" class="">}</span></div><div style="font-family: Menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="" class=""><br class=""></span></div><div style="margin: 0px; line-height: normal;" class=""><font face="georgia, serif" class="">My question: Is this normal? Should it all be automatic or not?</font></div></div></div></div></blockquote><br class=""></div><div>This is expected behavior: when you explicitly coerce (using ‘as’) or cast (using ‘as’ or ‘as!’), Bool can be converted to an NSNumber and from there up to AnyObject. Since NSNumber is part of Foundation, you need to have Foundation imported to do this.</div><div><br class=""></div><div>We’re still deciding whether the conversion from Bool to NSNumber (and similar "bridging conversions") should be implicit or explicit. The conversion from NSNumber to Bool has been explicit for a while now.</div><div><br class=""></div><div>Jordan</div><br class=""></body></html>