Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array as a dictionary value in swift language

Tags:

swift

I have the following swift dictionary

var List = [

   2543 : [ "book", "pen" ],
   2876 : [ "school", "house"]
]

How can i access the array values ?

println(List[2543][0]) 

The above code gives error "could not find member subscript"

and it should print "book"

like image 229
Knight Avatar asked Aug 10 '26 13:08

Knight


1 Answers

Note that subscript returns an optional. We have to force unwrapping:

println(list[2543]![0])

Or use optional chaining

println(list[2543]?[0])
like image 177
Sulthan Avatar answered Aug 13 '26 02:08

Sulthan