<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="">In that case, at least make it lazy:<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> allUInt8s = </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">UInt8</span><span style="font-variant-ligatures: no-common-ligatures" class="">.min.</span><span style="font-variant-ligatures: no-common-ligatures; color: #91540f" class="">stride</span><span style="font-variant-ligatures: no-common-ligatures" class="">(through: </span><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">UInt8</span><span style="font-variant-ligatures: no-common-ligatures" class="">.max, by: </span><span style="font-variant-ligatures: no-common-ligatures; color: #ff2600" class="">1</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><div style="margin: 0px; line-height: normal; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; line-height: normal; color: rgb(203, 203, 203);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #8f793a" class="">allUInt8s</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">dynamicType</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">// StrideThrough<UInt8>.Type</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(143, 121, 58);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #49a2d2" class="">Array</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">allUInt8s</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">).</span><span style="font-variant-ligatures: no-common-ligatures" class="">count</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #cbcbcb" class="">// 256</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(143, 121, 58);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">for</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> i </span><span style="font-variant-ligatures: no-common-ligatures; color: #c8ace5" class="">in</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">allUInt8s</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(203, 203, 203);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>i </span><span style="font-variant-ligatures: no-common-ligatures" class="">// 0...255</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">milos</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div><blockquote type="cite" class=""><div class="">On 8 Apr 2016, at 13:48, tuuranton--- via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">
<meta http-equiv="content-type" content="text/html; charset=UTF-8" class="">
<div class="">
<div class="">The problem with both solutions is that I lose the type information. Instead of i being UInt8 it is Int or UInt.</div><div class=""><br class=""></div><div class="">This is one solution I came up with:</div><div class=""><br class=""></div><div class="">extension UInt8 {</div><div class=""> static var allValues: [UInt8] {</div><div class=""> var v: [UInt8] = []</div><div class=""> for i in 0..<UInt8.max {</div><div class=""> v.append(i)</div><div class=""> }</div><div class=""> v.append(UInt8.max)</div><div class=""> return v</div><div class=""> }</div><div class="">}</div><div class=""><br class=""></div><div class="">print(UInt8.allValues)</div><div class=""><br class=""></div><div class="">In other words, loop from 0 to 254 and then add 255 manually at the end.</div><div class=""><br class=""></div><div class="">Is there currently no simpler way to do this in Swift?</div><div class=""><br class=""></div><div class="">Idea for the Swift developers: Create a Bounded protocol that UInt8 (and the other integer types) conform to. Then add a "allValues" type method in that protocol. And also put .min and .max into that protocol. Currently it seems like .min and .max are not a part of any particular protocol.</div><br class=""><br class="">8. Apr 2016 14:35 by <a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>:<br class=""><br class=""><blockquote class="tutanota_quote" style="border-left: 1px solid #93A3B8; padding-left: 10px; margin-left: 5px;"><div class="">You could cast the min and max values to an Int and iterate through the Int range:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px ; font-size: 16px ; line-height: normal ; font-family: "inconsolata" ; color: rgb( 164 , 176 , 177 )" class=""><span style="color: #de5194" class="">let</span><span class=""> min = </span><span style="color: #7f87cf" class="">Int</span><span class="">(</span><span style="color: #7f87cf" class="">UInt8</span><span class="">.min)</span></div><div style="margin: 0px ; font-size: 16px ; line-height: normal ; font-family: "inconsolata" ; color: rgb( 164 , 176 , 177 )" class=""><span style="color: #de5194" class="">let</span><span class=""> max = </span><span style="color: #7f87cf" class="">Int</span><span class="">(</span><span style="color: #7f87cf" class="">UInt8</span><span class="">.max)</span></div></div><div style="margin: 0px ; font-size: 16px ; line-height: normal ; font-family: "inconsolata" ; color: rgb( 164 , 176 , 177 )" class=""><span class=""><br class=""></span></div><div style="margin: 0px ; font-size: 16px ; line-height: normal ; font-family: "inconsolata" ; color: rgb( 164 , 176 , 177 )" class=""><span class=""></span><div style="margin: 0px ; line-height: normal" class=""><span style="color: #de5194" class="">for</span><span class=""> i </span><span style="color: #de5194" class="">in</span><span class=""> </span><span style="color: #2fafa9" class="">min</span><span class="">...</span><span style="color: #2fafa9" class="">max</span><span class=""> {</span></div><div style="margin: 0px ; line-height: normal" class=""><span class=""> </span><span style="color: #2c9edb" class="">print</span><span class="">(i)</span></div><div style="margin: 0px ; line-height: normal" class=""><span class="">}</span></div></div><div class=""><br class=""></div><div class="">While this will work it seems like a hack to me and Milos’ solution is more elegant…</div><div class=""><br class=""></div><div class="">-Pete</div><div class=""><br class=""></div><br class=""><div class=""><blockquote class="thunderbird_quote" style="border-left: 1px solid #93a3b8 ; padding-left: 10px ; margin-left: 5px"><div class="">On Apr 8, 2016, at 5:50 AM, tuuranton--- via swift-users <<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">
<div class="">
I forgot to mention that I really would like to have i have type UInt8 within the loop. And since i is UInt8 it makes sense it should be able to take the values UInt8.min and UInt8.max (and all the values between).<br class=""><br class="">8. Apr 2016 11:48 by <a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>:<br class=""><br class=""><blockquote class="tutanota_quote" style="border-left: 1px solid #93A3B8; padding-left: 10px; margin-left: 5px;"><p style="font-size: 11px ; line-height: normal ; font-family: "menlo"" class="">print(UInt8.min) //0</p><p style="font-size: 11px ; line-height: normal ; font-family: "menlo"" class="">print(UInt8.max) //255</p><p style="font-size: 11px ; line-height: normal ; font-family: "menlo"" class=""><br class=""></p><p style="font-size: 11px ; line-height: normal ; font-family: "menlo"" class="">//Is there an easy way to loop between all values</p><p style="font-size: 11px ; line-height: normal ; font-family: "menlo"" class="">//between (and including both) UInt8.min and UInt8.max?</p><p style="font-size: 11px ; line-height: normal ; font-family: "menlo"" class=""><br class=""></p><p style="font-size: 11px ; line-height: normal ; font-family: "menlo"" class="">//This doesn't work.</p><p style="font-size: 11px ; line-height: normal ; font-family: "menlo"" class="">//Runtime crash because UInt8.max has no successor.</p><p style="font-size: 11px ; line-height: normal ; font-family: "menlo"" class="">for i in UInt8.min...UInt8.max {</p><p style="font-size: 11px ; line-height: normal ; font-family: "menlo"" class=""> print(i)</p><p style="font-size: 11px ; line-height: normal ; font-family: "menlo"" class="">}</p><div class=""><br class=""></div></blockquote> </div>
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></div></blockquote></div><br class=""></blockquote> </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>