Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

range
swift
joined
communitycreator

What is ranges.joined() in Swift?

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The joined() method of a range is used to return a concatenated sequence of sequences. A range is a sequence with a lower bound and upper bound. It contains values from the lower bounds up to, but not including, the upper bound.

Syntax

range.joined()
Syntax for the joined() method of a range

Parameters

This method takes no parameters.

Return value

The value returned is a concatenated sequence of sequences.

Code example

// create some ranges
let range1 = [1...3, 6...9]
let range2 = [0..<3, 8..<10, 15..<17]
let concatenatedRange1 = range1.joined()
let concatenatedRange2 = range2.joined()
// print values in the concatenation
print("Concatenation 1")
for value in concatenatedRange1 {
print(value)
}
print("\nConcatenation 2")
for value in concatenatedRange2 {
print(value)
}
Get the concatenation of ranges in Swift using the joined() method

Explanation

  • Lines 2 and 3: We create two ranges, range1 and range2, that contain ranges.
  • Lines 5 and 6: We concatenate the ranges into a single value using the joined() method, and store the results in concatenatedRange1 and concatenatedRange2.
  • Lines 10 and 15: We print the values of the ranges using the for loop.

RELATED TAGS

range
swift
joined
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring