– [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 knowabout Swift ranges.I put together a playground into a projectfor an exercise file, which you can find in the downloads.So you can see here,our classic example of a rangeis 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 zeroto an end number of five,and uses the operator “dot dot dot.”Another thing that you’ve probably used beforeis 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 zeroand this time instead of dot dot dot,I have dot dot less than.And what that means, it’ll be one lessthan the end operator.So in this case, it’s actually zero to four.And if you run that, you’ll see we get tenwhich is five less, because we don’t have the five in there.
Okay, so that’s all pretty straight forwardand 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 rangeto 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 hereas 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 loopwhen 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 seethat we actually have a sub array createdfrom this range.You can also do something like a half open,so I can do eating equalsmy 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 thiswith 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 dessertsand 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 endand will pick all the ones that are on the end from two on.You can do it the other way as wellthat you can do: eatingequals my dessertsand do dot dot dot, and let’s go to three.So we can start at the beginning and go to threeand once again, we get a sub arraythat will have all the ones from,in subscript zero to subscript three.
You can also test for membershipwithin the contains property.So I can do range dotcontains and let’s say five.I can run that, and we’ll see it’s falsebecause 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 testif 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 rangeequal to zero, dot dot, and I’m going to actuallyuse counts here for this example.And I’m going to do var, eat equals blank,and if rangecontains A,eat equals my desserts,A, else,eat equals nothing.
And then I’ll put eat down hereand 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 pointif a half open or a closed range.
Open ranges make it very difficultto convert from one to another.Some API’s use range, and some use NS range.Make sure you know which one you’re using.