Nirupa
Nirupa

Reputation: 787

Contextual type 'Emoji' cannot be used with array literal

I'm trying to follow an exercise in the book App Development with Swift (Chapter 4 Tabel View).

The exercise tells me to add a property "emojis" of type [Emoji] to the viewControllerClass. The code is as follows:

 var emojis: [Emoji] = [
        [Emoji(symbol: "😀", name: "Grinning Face", description: "A typical smiley face.", usage: "happiness"),
         ]
    ]

But this line of code throws the error:

Contextual type "Emoji" cannot be used with array literal.

Upvotes: 2

Views: 328

Answers (2)

John Ure
John Ure

Reputation: 1

This shows the solution a bit more clearly:

var emojis: [Emoji] = [Emoji(symbol: "😀", 
                             name: "Grinning Face", 
                             description: "A typical smiley face.",
                             usage: "happiness"), 
                       Emoji(symbol: "😕", 
                            name: "Confused Face", 
                            description: "A confused, puzzled face.", 
                            usage: "unsure what to think; displeasure")]

Upvotes: 0

adev
adev

Reputation: 2092

Try this,

var emojis: [Emoji] = [Emoji(symbol: "😀", name: "Grinning Face", description: "A typical smiley face.", usage: "happiness")]

You are creating array of arrays. But you are var is declared as an array of Emoji type.

Upvotes: 2

Related Questions