【问题标题】:Contextual type 'Emoji' cannot be used with array literal上下文类型“表情符号”不能与数组文字一起使用
【发布时间】:2017-06-30 04:07:00
【问题描述】:

我正在尝试遵循 App Development with Swift(第 4 章表视图)一书中的练习。

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

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

但是这行代码抛出了错误:

上下文类型“Emoji”不能与数组文字一起使用。

【问题讨论】:

    标签: arrays swift xcode emoji


    【解决方案1】:

    试试这个,

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

    您正在创建数组数组。但是你是 var 被声明为 Emoji 类型的数组。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多