Junior — Middle
How is access to elements within a data structure performed?
sobes.tech AI
Answer from AI
In iOS, access to elements within a data structure depends on the type of the structure.
- For arrays (
Array), access is by index, for examplearray[0]. - For dictionaries (
Dictionary), access is by key, for exampledict["key"]. - For sets (
Set), there is no direct access by index, but you can iterate or check for the presence of an element.
Example:
let array = ["apple", "banana", "cherry"]
print(array[1]) // banana
let dict = ["name": "John", "age": "30"]
if let name = dict["name"] {
print(name) // John
}
let set: Set = [1, 2, 3]
if set.contains(2) {
print("Set contains 2")
}
``