<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">Thanks, Jordan.</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, May 26, 2016 at 12:33 AM, Jordan Rose <span dir="ltr"><<a href="mailto:jordan_rose@apple.com" target="_blank">jordan_rose@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><span class=""><br><div><blockquote type="cite"><div>On May 25, 2016, at 02:11, zh ao via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> wrote:</div><br><div><div dir="ltr"><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></div><div class="gmail_default"><div style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="color:rgb(187,44,162)">do</span><span> {</span></div><div style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span> </span><span style="color:rgb(187,44,162)">let</span><span> a:</span><span style="color:rgb(112,61,170)">Bool</span><span> = </span><span style="color:rgb(187,44,162)">true</span></div><div style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span> </span><span style="color:rgb(187,44,162)">let</span><span> object:</span><span style="color:rgb(112,61,170)">AnyObject</span><span> = a </span><span style="color:rgb(187,44,162)">as</span><span> </span><span style="color:rgb(112,61,170)">AnyObject</span></div><div style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span> object.</span><span style="color:rgb(187,44,162)">dynamicType</span></div><div style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span> </span><span>// __NSCFBoolean.Type</span></div><div style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span> </span><span style="color:rgb(187,44,162)">let</span><span> b:</span><span style="color:rgb(112,61,170)">Bool</span><span> = object </span><span style="color:rgb(187,44,162)">as</span><span>! </span><span style="color:rgb(112,61,170)">Bool</span></div><div style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span> b </span><span>// true</span></div><div style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span>}</span></div><div style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span><br></span></div><div 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></div><div style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><br></div><div style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span style="color:rgb(187,44,162)">do</span><span> {</span></div><div style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(112,61,170)"><span> </span><span style="color:rgb(187,44,162)">var</span><span> dictionary = </span><span>Dictionary</span><span><</span><span>String</span><span>, </span><span>AnyObject</span><span>>()</span></div><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span> </span><br></p><div style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span> </span><span style="color:rgb(187,44,162)">func</span><span> update<T:</span><span style="color:rgb(112,61,170)">AnyObject</span><span>>(value:</span><span style="color:rgb(79,129,135)">T</span><span>, key:</span><span style="color:rgb(112,61,170)">String</span><span>) {</span></div><div style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span> dictionary.</span><span style="color:rgb(61,29,129)">updateValue</span><span>(value, forKey: key)</span></div><div style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span> }</span></div><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span> </span><br></p><div style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span> </span><span style="color:rgb(187,44,162)">let</span><span> aBool = </span><span style="color:rgb(187,44,162)">true</span></div><div style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span> </span><span style="color:rgb(187,44,162)">let</span><span> key = </span><span style="color:rgb(209,47,27)">"testBool"</span></div><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span> </span><br></p><div style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span> dictionary.</span><span style="color:rgb(61,29,129)">updateValue</span><span>(aBool, forKey: key)</span></div><div style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span> </span><span>// works</span></div><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span> </span><br></p><div style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span> </span><span style="color:rgb(49,89,93)">update</span><span>(aBool, key: key)</span></div><div style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span> </span><span>// 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"><span> </span><br></p><div style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span> </span><span style="color:rgb(49,89,93)">update</span><span>(aBool </span><span style="color:rgb(187,44,162)">as</span><span> </span><span style="color:rgb(112,61,170)">AnyObject</span><span>, key:key)</span></div><div style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span> </span><span>// works</span></div><p style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal">
</p><div style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span>}</span></div><div style="font-family:Menlo;margin:0px;font-size:11px;line-height:normal"><span><br></span></div><div style="margin:0px;line-height:normal"><font face="georgia, serif">My question: Is this normal? Should it all be automatic or not?</font></div></div></div></div></blockquote><br></div></span><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></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><span class="HOEnZb"><font color="#888888"><div><br></div><div>Jordan</div><br></font></span></div></blockquote></div><br></div>