使用 (Int, Int) 元组案例在 Swift switch 语句中获取 "Binary operator ~= cannot be applied to two (Int, Int) operands"
Getting "Binary operator ~= cannot be applied to two (Int, Int) operands" in Swift switch statement with (Int, Int) tuple cases
我有一个 UITableViewController
和一个称为 IndexPath
的私有 enum
作为嵌套类型。
class SettingsViewController: UITableViewController {
enum IndexPath {
case Gender, Weight, EmergencyContact
var tuple: (Int, Int) {
switch self {
case .Gender:
return (0, 0)
case .Weight:
return (0, 1)
case .EmergencyContact:
return (1, 0)
}
}
}
}
这样做的目的是,在我的 cellForRowAtIndexPath
中,我可以简单地将节和行放在一个元组中,并制作一个 switch 语句来匹配枚举值。这样,我就可以为该索引路径自定义单元格:
switch (indexPath.section, indexPath.row) {
case IndexPath.Gender.tuple:
// Do something.
case IndexPath.Weight.tuple:
// Do something.
case IndexPath.EmergencyContact.tuple:
// Do something.
default:
break;
}
但是,我一直在 switch case 所在的行上收到编译器错误 Binary operator ~= cannot be applied to two (Int, Int) operands
。知道那是什么意思吗?我什至不知道运算符 ~=
,我也没有明确使用它。
这样做。
switch (indexPath.section, indexPath.row) {
case (IndexPath.Gender.tuple.0, IndexPath.Gender.tuple.1):
// Do something.
case (IndexPath.Weight.tuple.0, IndexPath.Weight.tuple.1):
// Do something.
case (IndexPath.EmergencyContact.tuple.0, IndexPath.EmergencyContact.tuple.1):
// Do something.
default:
break;
}
我有一个 UITableViewController
和一个称为 IndexPath
的私有 enum
作为嵌套类型。
class SettingsViewController: UITableViewController {
enum IndexPath {
case Gender, Weight, EmergencyContact
var tuple: (Int, Int) {
switch self {
case .Gender:
return (0, 0)
case .Weight:
return (0, 1)
case .EmergencyContact:
return (1, 0)
}
}
}
}
这样做的目的是,在我的 cellForRowAtIndexPath
中,我可以简单地将节和行放在一个元组中,并制作一个 switch 语句来匹配枚举值。这样,我就可以为该索引路径自定义单元格:
switch (indexPath.section, indexPath.row) {
case IndexPath.Gender.tuple:
// Do something.
case IndexPath.Weight.tuple:
// Do something.
case IndexPath.EmergencyContact.tuple:
// Do something.
default:
break;
}
但是,我一直在 switch case 所在的行上收到编译器错误 Binary operator ~= cannot be applied to two (Int, Int) operands
。知道那是什么意思吗?我什至不知道运算符 ~=
,我也没有明确使用它。
这样做。
switch (indexPath.section, indexPath.row) {
case (IndexPath.Gender.tuple.0, IndexPath.Gender.tuple.1):
// Do something.
case (IndexPath.Weight.tuple.0, IndexPath.Weight.tuple.1):
// Do something.
case (IndexPath.EmergencyContact.tuple.0, IndexPath.EmergencyContact.tuple.1):
// Do something.
default:
break;
}