Junior — Middle
What happens if you try to get a value from a dictionary using a non-existent key?
sobes.tech AI
Answer from AI
In iOS, a dictionary (NSDictionary or Swift Dictionary) returns nil (Swift) or nil-object (Objective-C) when trying to access a non-existent key.
This means the program won't crash, but you'll get a nil value, which should be handled to avoid errors.
Example in Swift:
let dict = ["key1": "value1"]
let value = dict["key2"] // value == nil
if let unwrappedValue = value {
print(unwrappedValue)
} else {
print("Key not found")
}