Posted On: Feb 22, 2018
Half-Open Range Operator: It is an operator (a<b) that is used to defines a range that runs from a to b but it doesn’t contain b. The reason for its name that is half-open is that it does not contains its last value, just have its first value. The value of 'a' compulsory is not greater than b in the closed ranged operator. The resulting range will be empty in the case of a is equal to b.
It is useful when you are working with zero-based lists like arrays where it is useful to calculate the length of the list. For example:
let names = ["Alley", "Anna", "Jack", "Brian"] let count = names.count for i in 0..<count span id="docs-internal-guid-b356581b-7fff-3109-a868-e60b14559aae">count print("Person \(i + 1) is called \(names[i])") } // Person 1 is called Alley, Person 2 is called Anna, Person 3 is called Jack, Person 4 is called Brian
Never Miss an Articles from us.