【问题标题】:Error: Cannot subscript a value of type 'inout [Agenda]' (aka 'inout Array<Agenda>')错误:无法为“inout [Agenda]”类型的值下标(又名“inout Array<Agenda>”)
【发布时间】:2016-06-19 12:21:20
【问题描述】:

我正在制作一个应用程序,当你按下一个按钮说它很紧急时,会有一个标签显示“紧急”。就在我实现与按钮的用户交互之前,我有一个数组(如下所示),其中一些对象具有urgent = true,但有些对象具有urgent = false,因此我可以从我的代码开始。

数组,在MainTableViewController.swift:

var content:[Agenda] = [
    Agenda(subject: "Read this article", deadline: "1-2 days", urgent: false),
    Agenda(subject: "Respond to this email", deadline: "ASAP", urgent: true),
    Agenda(subject: "Add this to diary", deadline: "When at home", urgent: true),
    Agenda(subject: "Listen to this song", deadline: "When finished working", urgent: false),
    Agenda(subject: "Check out this holiday destination", deadline: "At the weekend", urgent: false),
    Agenda(subject: "Download this podcast", deadline: "1-4 days", urgent: false),
    Agenda(subject: "Update notes", deadline: "When at home", urgent: true)
]

然后,我有一个名为 Agenda.swift 的类,我在其中声明了 subjectdeadlineurgent

这是我是否显示“紧急”的代码:

if content[indexPath.row].urgent = true {
        cell.urgentLabel.text = "URGENT"
    } else {
        cell.urgentLabel.text = ""
    }

在第一行,我收到以下错误:

不能为“inout [Agenda]”(又名“inout Array”)类型的值下标

【问题讨论】:

  • if content[indexPath.row].urgent = true { 应该只是 if content[indexPath.row].urgent {
  • 或 == 而不是 =(赋值语句在 Swift 中不返回任何内容)
  • @VinceBurn 谢谢 :)

标签: ios arrays swift uitableview


【解决方案1】:

这是经典的赋值运算符 (=) 与方程运算符 (==) 的混淆。

【讨论】:

  • 我对这个错误的解决方案是我需要将一个 Int 转换为一个字符串
  • 谢谢。我试图使用 += 的所有东西!
【解决方案2】:

当我将数组下标作为具有不正确签名的方法的参数值时,我刚刚遇到了同样的错误。也就是说,其中一个参数标签错误,但编译器将错误报告为无法为数组下标。一旦我将数组访问从方法调用中拉出来,真正的错误就暴露出来了。

【讨论】:

    【解决方案3】:

    正如 cmets 中部分解释的那样,这一行

    if content[indexPath.row].urgent = true {
    

    正在尝试设置紧急的值,而不是检查紧急的值。

    如果您想使用该模式,您只需添加一个额外的 = 符号

    if content[indexPath.row].urgent == true {
    

    另外,if 语句将直接评估布尔值,因此您甚至不需要比较

    if content[indexPath.row].urgent {
    

    【讨论】:

    • 感谢您的贡献。
    【解决方案4】:

    对于 Swift3 问题:我有一本没有的字典

     as [String:Any]
    

    最后,编译器告诉我我不能下标 inout 对象。最后加上了,boom,解决了。

    【讨论】:

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