We can check if a range is empty in Swift by using the isEmpty
property of a Range
instance. It returns a boolean true
or false
indicating if the range is empty or not.
range.isEmpty
The value returned is a boolean variable. If the range is empty, true
is returned. Otherwise, it returns false
.
// create some rangeslet range1 = 1 ..< 6 // 1 to 5let range2 = 10 ..< 50 // 10 to 49let range3 = 0 ..< 0 // emptylet range4 = 3 ..< 3 // empty// check ranges if they are emptyprint(range1.isEmpty) // falseprint(range2.isEmpty) // falseprint(range3.isEmpty) // trueprint(range4.isEmpty) // true
isEmpty
property. Then, we print the result to the console.