iOS Development Tips Weekly

– [Instructor] You’ve probably used ranges and loops, but have you ever thought about their other powers? This time, I’ll show you a few things you might want to know about Swift ranges. I put together a playground into a project for an exercise file, which you can find in the downloads. So you can see here, our classic example of a range is a four loop, like this one. I can go ahead and run this, and it just goes through the numbers zero through five. And this is what’s known as a closed range, it goes from a beginning number of zero to an end number of five, and uses the operator “dot dot dot.” Another thing that you’ve probably used before is the half open range.

Which is what I’ve got here. So again, it’s number in, and then the range is over here, and for this range I’ve got a zero and this time instead of dot dot dot, I have dot dot less than. And what that means, it’ll be one less than the end operator. So in this case, it’s actually zero to four. And if you run that, you’ll see we get ten which is five less, because we don’t have the five in there.

Okay, so that’s all pretty straight forward and you’ve probably seen all that before. Now open and closed ranges are their own type of range, which is not to be confused with NS range. I can assign a range like any other type. So I can go ahead and I can do var, range, equals zero, dot dot, less than five. And then I can actually take this up here, copy, paste, and change the range to range and it’ll work just as fine.

You can see here, if we look at the inspector, that it’s giving me a lower bound and an upper bound here as the assigned object. And once again, it ran it five times. Just like it did here. Now, keeping just integers for now, here’s one of the really cool things you can do with it, is, I don’t even need a four loop when I’m using ranges with arrays. So for example, let me put this in: var, eating, equals my desserts, and instead of putting a single subscript in here, I’m going to put two, dot dot dot, four.

I put a range in there. And if I run that, you’ll see that we actually have a sub array created from this range. You can also do something like a half open, so I can do eating equals my desserts, two, dot dot, less than four, run that, I’ll pop that out, and now I just get the rosettes of the pecan pie, bunelos.

Alright, so, with that, it gets really interesting. Now you might want to sometimes stick count on the end here, but one of the neat things about when doing this with subscripts, is you don’t need the count. That’s what’s known as an open range. And so you can do something like this: eating equals my desserts and I’m going to start with two, and I’m just going to put three dots and nothing on the end.

Watch what happens. It goes and runs and goes to the end and will pick all the ones that are on the end from two on. You can do it the other way as well that you can do: eating equals my desserts and do dot dot dot, and let’s go to three. So we can start at the beginning and go to three and once again, we get a sub array that will have all the ones from, in subscript zero to subscript three.

You can also test for membership within the contains property. So I can do range dot contains and let’s say five. I can run that, and we’ll see it’s false because I only go to four. And I can also do range, contains, and it’s asking me for an element, let’s do two, which we know is in there. And if I run that, I get true.

Which means you can use it to test if an index is within a range. So I’m going to do something fun like this: let A equals four, and I’ll set the range equal to zero, dot dot, and I’m going to actually use counts here for this example. And I’m going to do var, eat equals blank, and if range contains A, eat equals my desserts, A, else, eat equals nothing.

And then I’ll put eat down here and I’ll run this, and it tells me it’s ice cream. I change that to 42, and it tells me nothing. So one important caution as I mentioned earlier, unlike other Swift types, range and NS range is not the same thing. NS range is a start point and a distance in a range. Range is a start and possibly a stop point if a half open or a closed range.

Open ranges make it very difficult to convert from one to another. Some API’s use range, and some use NS range. Make sure you know which one you’re using.

APT eLearning Translations Source |