Junior — Middle
What type of data is a dictionary where keys are strings and values can be of different types?
sobes.tech AI
Answer from AI
In iOS (Swift), a dictionary where keys are strings and values can be of different types is usually represented by the type Dictionary<String, Any>.
Here:
Stringis the type of the keys.Anyis a universal type that can contain values of any type.
Example:
var dict: [String: Any] = [
"name": "Ivan",
"age": 30,
"isMember": true
]
print(dict["name"] as? String ?? "") // Ivan
Using Any allows storing values of different types in the dictionary but requires type casting when retrieving.