<div dir="ltr">I would swear I had marked the optional initially with the same result, but it works, so apparently I didn&#39;t!</div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Apr 21, 2016 at 1:49 PM, Dennis Weissmann <span dir="ltr">&lt;<a href="mailto:dennis@dennisweissmann.me" target="_blank">dennis@dennisweissmann.me</a>&gt;</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">Since sublayers are optional you’re calling the the flatMap function on Optionals, which behaves differently.<div><br></div><div>let layers = myView.layer.sublayers?.flatMap({ $0 as? CAShapeLayer })<span style="white-space:pre-wrap">                </span>// sublayers?. instead of sublayers.</div><div><br></div><div>should fix the problem :)</div><div><br></div><div><div>
<div style="color:rgb(0,0,0);letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div>- Dennis</div></div>

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