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 ranges let range1 = 1 ..< 6 // 1 to 5 let range2 = 10 ..< 50 // 10 to 49 let range3 = 0 ..< 0 // empty let range4 = 3 ..< 3 // empty // check ranges if they are empty print(range1.isEmpty) // false print(range2.isEmpty) // false print(range3.isEmpty) // true print(range4.isEmpty) // true
isEmpty
property. Then, we print the result to the console.RELATED TAGS
CONTRIBUTOR
View all Courses