上下文类型'表情符号'不能与数组文字一起使用

时间:2017-06-30 04:07:00

标签: arrays swift xcode emoji

我正在尝试按照 App Development with Swift (第4章表格视图)中的练习进行练习。

练习告诉我将[Emoji]类型的属性“表情符号”添加到viewControllerClass。代码如下:

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

但是这行代码会引发错误:

  

上下文类型“表情符号”不能与数组文字一起使用。

2 个答案:

答案 0 :(得分:2)

试试这个,

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

您正在创建数组数组。但是你将var声明为表情符号类型的数组。

答案 1 :(得分:0)

这更清楚地显示了解决方案:

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")]