Junior — Middle
What methods of working with and interacting with data collections do you know?
sobes.tech AI
Answer from AI
In iOS, standard data structures such as arrays (Array), sets (Set), and dictionaries (Dictionary) are used for working with collections of data. Various methods and protocols are applied to interact with them.
Main ways to work with collections:
- Iteration: using for-in loops, forEach methods.
- Filtering: the filter method allows selecting elements based on a condition.
- Transformation: map is used to transform collection elements.
- Sorting: the sorted method sorts elements according to a specified criterion.
- Searching: first(where:) returns the first element satisfying a condition.
Example:
let numbers = [1, 2, 3, 4, 5]
// Filtering even numbers
let evenNumbers = numbers.filter { $0 % 2 == 0 }
// Transformation: squaring numbers
let squares = numbers.map { $0 * $0 }
// Iteration
for number in numbers {
print(number)
}
For more complex tasks, you can also use collections from the Swift Standard Library or third-party libraries, such as for reactive programming (Combine, RxSwift).