<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><blockquote type="cite" class=""><div class="">On Jun 30, 2017, at 2:44 PM, David Baraff 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=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Python:</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">&nbsp;shortID = longerDeviceID[-2:]</span><span class="Apple-tab-span" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: pre; word-spacing: 0px; -webkit-text-stroke-width: 0px;">        </span><span class="Apple-tab-span" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: pre; word-spacing: 0px; -webkit-text-stroke-width: 0px;">        </span><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class=""># give me the last two characters</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Swift:</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">&nbsp;let shortID = String(longerDeviceID.characters.dropFirst(longerDeviceID.characters.count - 2))</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""></div></blockquote><br class=""></div><div>This actually perfectly illustrates why Swift is designed the way it is.</div><div><br class=""></div><div>The first thing to notice is that the Python version is terse, but it's actually impossible to know what it actually *does*. Is it operating on bytes, code units, Unicode scalars, or grapheme clusters? Well, that depends: is this Python 2 or Python 3? If it's 2, is this a `string` or a `unicode`? If it's 3, is it a `string` or a `bytes`? And if it is one of the Unicode-aware types, how are those indexed? (I honestly don't know—I can't find anything about that in the documentation.) And forget about understanding its performance characteristics—that's an implementation detail.</div><div><br class=""></div><div>The second thing to notice is that your Swift solution is very inefficient. It counts all the characters in the string, subtracts two, then counts all but the last two characters in the string before returning the last two. That is, it unnecessarily walks over the entire string twice. If you read your expression closely, all of this behavior is plainly stated in the code.</div><div><br class=""></div><div>What you actually want to do is count back two characters from the *end*, like so:</div><div><br class=""></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>let shortID = String(longerDeviceID.characters[longerDeviceID.characters.index(longerDeviceID.characters.endIndex, offsetBy: -2) ..&lt; longerDeviceID.characters.endIndex])</div><div><br class=""></div><div>Or, in Swift 4:</div><div><br class=""></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>let shortID = String(longerDeviceID[longerDeviceID.index(longerDeviceID.endIndex, offsetBy: -2)...])</div><div><br class=""></div><div>Or, as Adrian Zubarev pointed out, you can use the very convenient `suffix(_:)` method, available on any `Sequence`:</div><div><br class=""></div><div><span class="Apple-tab-span" style="white-space: pre;">        </span>let shortID = String(longerDeviceID.characters.suffix(2))<span class="Apple-tab-span" style="white-space:pre">        </span>// Swift 3</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>let shortID = String(longerDeviceID.suffix(2))<span class="Apple-tab-span" style="white-space:pre">                                </span>// Swift 4</div><div><br class=""></div><div>Swift's strings were very deliberately designed this way. It's tougher to get off the ground, sure, but it's better in the long run.</div><div><br class=""></div><div class="">
<span class="Apple-style-span" style="border-collapse: separate; font-variant-ligatures: normal; font-variant-east-asian: normal; font-variant-position: normal; line-height: normal; border-spacing: 0px;"><div class=""><div style="font-size: 12px; " class="">--&nbsp;</div><div style="font-size: 12px; " class="">Brent Royal-Gordon</div><div style="font-size: 12px; " class="">Architechies</div></div></span>

</div>
<br class=""></body></html>