Junior — Middle
Can you list the main data structures and tell about them?
sobes.tech AI
Answer from AI
The main data structures in iOS (Swift) include:
- Array — an ordered list of elements of the same type. Fast access by index.
- Dictionary — a collection of key-value pairs, where keys are unique. Allows quick lookup of values by key.
- Set — an unordered collection of unique elements. Used for membership testing and removing duplicates.
- Tuple — a fixed group of values of different types, combined together.
Example usage:
var numbers: [Int] = [1, 2, 3]
var userInfo: [String: String] = ["name": "Ivan", "city": "Moscow"]
var uniqueNumbers: Set<Int> = [1, 2, 3, 3]
let person: (name: String, age: Int) = ("Anna", 30)
In addition to standard structures, data structures from Foundation are often used:
- NSArray, NSDictionary, NSSet — Objective-C classes compatible with Swift.
- LinkedList, Stack, Queue — can be implemented independently if needed.
Each data structure is suitable for different tasks: arrays for ordered data, dictionaries for quick search, sets for unique elements.