Reputation: 665
So I stored several elements, namely, an array of String at a particular key in a dictionary that I created. However, I could not find anything, including the apple developer's guide, regarding accessing a particular element in a dictionary's key. Looking at the code below might make more sense.
import UIKit
var dict = Dictionary<Int, Array<String>>()
dict[1] = ["dog", "cat", "fish"]
dict[1]! += ["poop"]
dict[1]!.append("snacks")
print(dict[1]) //"[dog, cat, fish, poop, snack]\n"
So I want to access dog
using a 2d like syntax, e.g., dict[1][0]
. So I tried things like, dict.keys[1].Array[0]
, dict.keys.array[0]
, and dict[1: [0]]
in order to access dog
. However, none of these work. Anybody have a solution to this problem? Thank you in advance!
Upvotes: 1
Views: 44
Reputation: 4595
Dictionary subscript in Swift returns optional, so you need to unwrap it:
print(dict[1]![0])
Upvotes: 2