【问题标题】:Calling specific index to add a function for that index调用特定索引为该索引添加函数
【发布时间】:2017-01-12 07:29:26
【问题描述】:

我有一个liquidFloatingActionButton,您添加操作的方式是一个didSelectItemAtIndex 函数。但是如何调用某些索引为每个不同的索引添加不同的功能呢?

这是我到目前为止所拥有的,但我得到的错误是:

“无法赋值:'index' 是一个 'let' 常量”。

func liquidFloatingActionButton(_ liquidFloatingActionButton: LiquidFloatingActionButton, didSelectItemAtIndex index: Int) {
    print("did Tapped! \(index)")

    if index = "0"{
        print("Buy")
    }else if index = "1"{
        print("Trade")
    }else if index = "2"{
        print("Save")
    }else if index = "3"{
        print("Message")
    }else if index = "4"{
        print("Directions")
    }

    liquidFloatingActionButton.close()
}

【问题讨论】:

  • 您应该使用 == 而不是 = ,否则您将该值分配给索引。此外, index 不是字符串而是 int 所以if index == 1
  • 我这样做没用
  • 我在上面添加了一张图片,这样你就可以看到我在说什么
  • 错误是您将字符串与 int 进行比较。这就像将同行与苹果进行比较。使用 1 而不是“1”

标签: swift


【解决方案1】:

有 2 个错误。第一个是您使用的是Assignment Operator (=) 而不是Comparison Operators (==)。另一个错误是您正在将字符串与 int 进行比较。 更多关于基本运算符here

 func liquidFloatingActionButton(_ liquidFloatingActionButton: LiquidFloatingActionButton, didSelectItemAtIndex index: Int) {
        print("did Tapped! \(index)")

     if index == 0{
        print("Buy")
    }else if index == 1{
        print("Trade")
    }else if index ==  2{
        print("Save")
    }else if index == 3{
        print("Message")
    }else if index == 4{
        print("Directions")
    }

    liquidFloatingActionButton.close()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    • 2014-09-05
    • 1970-01-01
    • 2012-06-01
    • 2016-03-29
    相关资源
    最近更新 更多