Make App Pie

Training for Developers and Artists

Ranges in Swift

You’ve probably used ranges in loops, without knowing it, but ranges are really a type in Swift. Have you ever thought about ranges and all their power? I’ll show you a few things you might want to know about Swift ranges. I’ve put a playground into a Project for an exercise file. I added there an array to play with

Here’s the one place I’m sure you’ve seen a range: in a for loop. Commonly you’ll see them as a closed range using the ... operator, which include all values in the range. This code

Adds 0+1+2+3+4+5 to get 15

A half-open range takes one less than the number, by changing the last dot for a less than symbol(..<). This half-open example goes from 0 to 4. Which gives us a total of 10.

Open and close ranges are their own type Range, not to be confused with NSRange. I can assign a range like any other type. Note here the upper bound is outside the range. .

Then use that value as the range in my for loop.

You can also use a range directly in an array’s subscript. I can make a subarray of elements like this.

which makes a small array of the second, third, and fourth elements of the array.

You can use an half open range here too.   

You can stick a count for the array in the range, but there is a faster way. You can use fully open range. Open ranges don’t specify one bound of the range. For example to the end of the array like this: 

Or the beginning to iterate from the beginning to an end. 

You also can test for membership with the contains property. To see if my range contains 5 I’d use

Since I set range= 0..<5 this returns false as 5 is outside the range. If I look for 2

that returns true because 2 is between 1 and 4

the contains method become handy because you can use it to test if an index is within a range. For a simple example, I’ll write a snippet of code to check if an index is in a given array. I’ll check for element 4 and get Ice cream.

I check for 42, I’ll get nothing

I’ve stuck to Integers here, but ranges can be other types as well. Four example, you can add a range of Double, then check if a valur exists inthat range quickly.


One important caution I mentioned earlier: Unlike other Swift types, Range and NSRange is not the same thing.

NSRange is a location and a length , Range is upper and lower bounds. Closed ranges might be a bit easier to convert between the two, but open ranges make it very difficult to convert from one to another. Some API use Range and some NSRange. Make sure you know which one you are using.  

The Whole Code

You can also download this from Github here: If you copy and paste this into a Swift playground in iPad, the copy below will format itself.

import UIKit
//:# Range Demo
//
//:  A Demo for iOS Development Tips Weekly
//:  by Steven Lipton (C)2018, All rights reserved
//:  For videos go to http://bit.ly/TipsLinkedInLearning
//:  For code go to http://bit.ly/AppPieGithub
//:

//: A swift playground formatted for iPad playgrounds

let myDesserts = ["Cannoli","Mochi","Bunelos","Pecan Pie","Ice Cream"]

//:## Basic Ranges
//: Closed range
var total = 0
for number in 0...5{
    total += number
}
total


//: Half Open range
total = 0
for number in 0..<5{
    total += number
}
total


var doubleRange = 0.1...6.7
doubleRange.contains(Double.pi)




//: Assigning ranges
var range = 0..<5
//:Range is not NSRange
var nsRange = NSRange(location: 0, length: 4)






total = 0
for number in range{
    total += number
}
total




//: Use as array subscripts
var eating  = myDesserts[2...4]
eating = myDesserts[2..<4]
eating = myDesserts[2...]
eating = myDesserts[...3]
//: Membership
range.contains(5)
range.contains(2)


let a = 42
range = 0..<myDesserts.count
var eat = ""
if range.contains(a){
    eat = myDesserts[a]
} else {
    eat = "nothing"
}
eat


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: