Reputation: 787
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
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
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