<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="">Since sublayers are optional you’re calling the the flatMap function on Optionals, which behaves differently.<div class=""><br class=""></div><div class="">let layers = myView.layer.sublayers?.flatMap({ $0 as? CAShapeLayer })<span class="Apple-tab-span" style="white-space:pre">                </span>// sublayers?. instead of sublayers.</div><div class=""><br class=""></div><div class="">should fix the problem :)</div><div class=""><br class=""></div><div class=""><div class="">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">- Dennis</div></div>

</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On Apr 21, 2016, at 9:53 PM, Nate Birkholz 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="">myView.layer.sublayers returns an array of CALayer objects. Some of my sublayers are CAShapeLayers. I want to act on the CAShapeLayer objects.<div class=""><br class=""></div><div class="">Shouldn't flatMap allow me to conditionally cast CALayers to CAShapeLayers? i.e.&nbsp;</div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><div class="">let layers = myView.layer.sublayers.flatMap({ $0 as? CAShapeLayer })</div><div class=""><br class=""></div></blockquote>The compiler warns "Cast from '[CALayer]' to unrelated type 'CAShapeLayer' always fails"<div class=""><br class=""></div><div class="">So it looks like $0 is the array itself.&nbsp;<br class=""><div class=""><br clear="all" class=""><div class="">This works:&nbsp;</div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><div class=""><div class="">let layers = myView.layer.sublayers?.filter({ $0 is CAShapeLayer }).map({ $0 as? CAShapeLayer })&nbsp;<br class=""></div></div></blockquote><div class=""><div class=""><br class=""></div><div class="">But I thought FlatMap should do it in a single step.</div><div class=""><br class=""></div>-- <br class=""><div class="">Nate Birkholz</div>
</div></div></div>
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></div></body></html>