<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=""><div style="margin: 0px; line-height: normal;" class="">Hi everyone!</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">I’m working on the following method:</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(222, 52, 140);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">SequenceType</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(192, 192, 192);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">/// Check if `predicate` is true for all elements of `self`</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c0c0c0" class="">///</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(192, 192, 192);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">/// - Parameter predicate: The predicate called on each element of `self`</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #c0c0c0" class="">///</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(192, 192, 192);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">/// - Returns: True iff every element in `self` satisfies `predicate`, false otherwise</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(222, 52, 140);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">@warn_unused_result</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> all(</span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">@noescape</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">where</span><span style="font-variant-ligatures: no-common-ligatures" class=""> predicate: Generator.Element </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">throws</span><span style="font-variant-ligatures: no-common-ligatures" class=""> -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">Bool</span><span style="font-variant-ligatures: no-common-ligatures" class="">) </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">rethrows</span><span style="font-variant-ligatures: no-common-ligatures" class=""> -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">Bool</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">for</span><span style="font-variant-ligatures: no-common-ligatures" class=""> element </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">in</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">where</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">try</span><span style="font-variant-ligatures: no-common-ligatures" class=""> !predicate(element) {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">false</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(222, 52, 140);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">return</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">true</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">}<span style="font-variant-ligatures: no-common-ligatures" class=""></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; min-height: 13px;" class="">However, when the sequence is empty the method returns true, which is not the desired behaviour.</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; line-height: normal; min-height: 13px;" class=""><div style="margin: 0px; line-height: normal;" class=""><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> a = [</span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures" class="">]()</span></div><div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> b = a.</span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">all</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">where</span><span style="font-variant-ligatures: no-common-ligatures" class="">: { $0 > 7 })</span></div><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(88, 126, 168); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">XCTAssertFalse</span><span style="color: rgb(0, 0, 0); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">(b) </span><font color="#c0c0c0" face="Menlo" class=""><span style="font-size: 11px;" class="">// This fails, cause there’s no guard against an empty sequence</span></font></div></div><div style="font-family: Menlo; font-size: 11px;" class=""><br class=""></div><div class=""><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">Does anyone know how to </span></span>guard against an empty sequence<span style="font-family: Helvetica; font-size: 12px;" class="">?</span></div><div style="font-family: Menlo; font-size: 11px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">I’m using Xcode 7.3.1 and Swift 2.2.</span></div><div style="font-family: Menlo; font-size: 11px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></span></div><div style="font-family: Menlo; font-size: 11px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">Best,</span></span></div><div style="font-family: Menlo; font-size: 11px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">— A</span></div></div></body></html>