我正在制作一个应用程序,当你按下一个按钮说它是紧急的,有一个标签上写着“紧急”。在实现用户与按钮的交互之前,我有一个数组(如下面所示),其中一些对象具有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的类,其中声明了subject、deadline和urgent。
下面是我显示“紧急”或“不紧急”的代码:
if content[indexPath.row].urgent = true {
cell.urgentLabel.text = "URGENT"
} else {
cell.urgentLabel.text = ""
}在第一行中,我得到以下错误:
无法下标“inout Agenda”类型的值(又名“inout Array”)
发布于 2016-03-05 19:14:14
这是经典的赋值算子(=)与方程算子(==)的混淆。
发布于 2017-01-04 16:07:22
当我将数组作为参数值订阅到一个签名不正确的方法时,我遇到了同样的错误。也就是说,其中一个参数标签是错误的,但是编译器将错误报告为无法订阅数组。一旦我将数组访问从方法调用中提取出来,就会发现真正的错误。
发布于 2016-03-05 19:15:30
正如评论中部分解释的那样,这一行
if content[indexPath.row].urgent = true {正在尝试设置紧急值,而不是检查紧急值。
如果要使用该模式,只需添加一个附加=符号
if content[indexPath.row].urgent == true {另外,if语句将直接计算布尔值,因此甚至不需要比较
if content[indexPath.row].urgent {https://stackoverflow.com/questions/35818524
复制相似问题