2017-06-30 31 views
1

我正在嘗試使用Swift(第4章表格視圖)中的練習上下文類型「表情符號」不能與數組文字一起使用

練習告訴我將[Emoji]類型的屬性「emojis」添加到viewControllerClass。代碼如下:

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

但是這行代碼引發錯誤:

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

回答

1

試試這個,

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

您正在創建數組的數組。但是你被var聲明爲Emoji類型的數組。

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")] 
相關問題