Bring Back Swift’s Old Half-closed Range Operator

Swift

If you’re one of the many (many) people who dislike the change Apple made to the half-closed range operator, then rejoice, because with this snippet of code you can bring it back:

[Edit: there’s a better version of this code at the end, see it now.]

And then you can use it just like before:

This really just shows the power of Swift, in that we can define a lot of how we want it to work ourselves.

Here’s an updated version which is more concise:

 

 

Kane Cheshire

Author: Kane Cheshire

I'm a software developer (and sort of designer) from Hertfordshire. I spend a lot of time learning, travelling and flying my drone. I made MacID. You can check out some of my favourite photographs from travelling at http://kane.codes. I'm also the creator and lover of Twicnic, an annual Twitter picnic event.

4 thoughts on “Bring Back Swift’s Old Half-closed Range Operator”

  1. I think that sort of thing is dangerous though. The original option of .. and … was not a good idea from the start, and I don’t think much of their replacement. But adding it back in is just reintroducing the same bug in waiting.

    What I think is needed is a range that has start and length. That is what the .. loop was trying to do.

    1. I don’t think it’s dangerous per se, I’ve never had any issues with .. vs …, but obviously enough people did for Apple to change it. The wonderful thing is that Swift allows us to redefine it if we want.

      You’re right about using a Range though, I’ve updated the code to return a Range instead.

      [EDIT] Just re-read your comment after a coffee and realised what you meant. Still, the new code is more concise regardless!

Leave a Reply

Your email address will not be published. Required fields are marked *