【问题标题】:Binary operator error on condition条件二元运算符错误
【发布时间】:2018-02-22 11:11:29
【问题描述】:

我想在 pickerview 行上给出条件,但它只是给了我错误。 二元运算符不能应用于操作数

let viewList: [String] = ["Fixed", "Recurring"]

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    if (viewList[row] == 0) {

    }
}

【问题讨论】:

  • 请帮帮我
  • 您要查看row == 0吗?您现在正在将字符串与数字进行比较。

标签: ios swift3 uipickerview


【解决方案1】:

更新答案

尝试这样做:

class MyViewController: UIViewController {
    fileprivate let viewList: [String] = ["Fixed", "Recurring"]
}

// MARK: - UIPickerViewDelegate
extension MyViewController: UIPickerViewDelegate {
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
            print("Selected value \(viewList[row]) at row \(row)")

            if viewList[row] == "Fixed" {
                print("Fixed has been selected!")
            }
    }
}

问题来自你的情况,试试下面的代码。

更多信息:Apple Documentation - Collection Types

【讨论】:

  • var viewList : [String] = ["Fixed","Recurring"] func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return viewList.count } func pickerView(_ pickerView:UIPickerView,titleForRow 行:Int,forComponent 组件:Int)-> String? { self.view.endEditing(true) return viewList[row] } func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { }
  • 我已经尝试过你声明数组的方式,但它仍然给出同样的错误
  • 我正在使用 swift3
  • 好的,我明白了!如果您想检查用户是否触摸了第一个单元格,只需执行if row == 0 { print("Touched row 0") },但如果您想检测用户是否选择了值为“Fixed”的单元格,只需执行if viewList[row] == "Fixed" { print("Touched row Fixed") },但要小心,以防您的应用程序需要内部化。
  • @ShaybazSayyed 如果这解决了你的问题,请接受答案:)
猜你喜欢
  • 1970-01-01
  • 2023-01-26
  • 1970-01-01
  • 1970-01-01
  • 2013-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多