0

I have the following dictionary:

var deckDictionary = [
            "card1": ["ace","hearts"],
            "card2": ["ace","spades"],
            "card3": ["ace","diamonds"],
            "card4": ["ace","clubs"],
]

What I want to do is set cardSuit2 to be equal to the 2nd object in the array with key "card2" like so (pseudo-code):

var cardKey2 = "card2"
var cardSuit2 = whatever the card suit value is at cardKey2 ([[deckDictionary[1]][1]]?)

So for instance in this case, cardSuit2 would be equal to the 2nd object in the array with key "card2", so "spades". How would I go about doing this?

4
  • 1
    What did you try and what went wrong? Commented Oct 28, 2015 at 19:50
  • @Wain I tried var cardSuit1 = ([[deckDictionary[1]][1]]), and it gives me an error stating Cannot subscript a value of type '[String : Array<String>]' with an index of type 'Int' Commented Oct 28, 2015 at 21:07
  • @ghobs91what about var cardSuit1 = deckDictionary["card2"]![1] Commented Oct 28, 2015 at 21:21
  • @ghobs9 Just make sure that you are indexing the dictionary with a key you know will exist. If that is not the case, you'll need to check if deckDictionary["card2"] is nil. Commented Oct 28, 2015 at 21:22

1 Answer 1

1

If you just want to get the value of the element that is pointed out in the question it is possible to do so:

var cardKey2 = "card2"
var cardSuit2 = deckDictionary[cardKey2]?.last
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.