Why this Key is an optional type?


I've assigned a value to the Key in the dictionary. But why Xcode report it's an optional type? Thx.

It's simply because the key might be missing in the dictionary:

contactList["Shah"] // .some("...")
contactList["Bob"]  // .none aka nil

BTW, there's this version of subscript that returns non-optional:

contactList["Bob", default: "123"]  // "123"
3 Likes