6

What I want to do is set the image of firstCard to be equal to the image file with the name corresponding to firstCardString.

For instance, in the case below, the code could be setting self.firstCard.image to show the image named "card1" if it randomly picks it (I've clipped the rest of the array for brevity, the full thing contains 52 objects).

var deckArray = [
        "card1": ["Bear","Ball"],
        "card2": ["Bear","Ball"],
        "card3": ["Bear","Ball"],
        "card4": ["Bear","Ball"],
        "card5": ["Bear","Ball"],
        "card6": ["Bear","Ball"],
        "card7": ["Bear","Ball"],
]

    let firstRandomNumber = Int(arc4random_uniform(52))+1
    let firstCardString = deckArray["card\(firstRandomNumber)"]
    self.firstCard.image = UIImage(named: firstCardString)

Instead, I'm getting the following error:

Cannot convert value of type '[String]?' to expected argument type 'String'

I'm not entirely sure what this error message means, what is [String]??

3 Answers 3

7

[] is an array, and ? is an optional. Your firstCardString is not a string at all, but an optional array of strings. You've read the deckArray dictionary's value for that card, you see, and so your firstCardString actually looks something like this: Optional(["Bear", "Ball"]). I think you meant:

self.firstCard.image = UIImage(named: "card\(firstRandomNumber)")

This will set the image based on strings like "card1" or "card4". I assume you'll use your dictionary for something else, later. When you do, be sure to unwrap the optional value it returns:

if let cardArray = deckArray["card\(firstRandomNumber)"] {
    //do something with bears and balls
}

Alternatively, consider making deckArray an array (which will make the name more reasonable), rather than a dictionary. Then you won't have to deal with optionals and will be able to access items as follows:

let cardArray = deckArray[firstRandomNumber]
//do something with bears and balls
Sign up to request clarification or add additional context in comments.

3 Comments

But if I use self.firstCard.image = UIImage(named: "card\(firstRandomNumber)"), won't that attempt to set firstCard to an image name which is just a random number between 1 and 52, rather than setting it to the key ("card4") that corresponds to the index with that number?
The image name will be "card1" or "card3" or "card5". See the word "card" there in the name? It's followed by \(firstRandomNumber), which will insert the number right after the word "card".
@andyvn22 please answer the same related question here stackoverflow.com/questions/42733225/…
3

Your deckArray is a Dictionary, and your firstCardString is an Array.

String = String

[String] = Array of strings.

1 Comment

please answer the similar related question here stackoverflow.com/questions/42733225/…
0

It seems like deckArray is in fact, a dictionary of arrays of strings. Therefore if firstRandomNumber = 1, deckArray["card\(firstRandomNumber)"] will return ["Bear","Ball"]. That is definitely not a string!

1 Comment

please answer the similar related question here stackoverflow.com/questions/42733225/…

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.