Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anyone seen the 'some' output in playground when using dictonaries?

Tags:

swift

I am just writing some simple dictionary code as

var picCard: Dictionary<String, Int> = ["jack": 11, "Queen": 12, "King": 13]

But when I access one of the entries in the dict in the playground like

picCard["Jack"]

The output gives me:

{some 11}

Been through the swift programming guide and cant find out why it says 'some'

like image 447
garyk1968 Avatar asked Sep 30 '22 14:09

garyk1968


1 Answers

Those are optionals. Optional is basically defined like this:

enum Optional<T> {
    case None
    case Some(T)
    // ...
}

An optional with a value is Some <value>, nil is None:

var foo: String = "blah"  // "blah"
var bar: String? = "bleh"   // {Some "bleh"}

In your case, subscripting a Dictionary returns an optional value, because the key might not exist.

like image 143
Lukas Avatar answered Oct 03 '22 02:10

Lukas