【问题标题】:Binary operator '>=' cannot be applied to operands of type 'Any' and 'Int'二元运算符“>=”不能应用于“Any”和“Int”类型的操作数
【发布时间】:2017-04-11 19:55:34
【问题描述】:

我在 tableview 的 viewforHeaderInSection 方法中应用了以下代码。检查dictkey >= 3时出现错误:

二元运算符“>=”不能应用于“Any”和“Int”类型的操作数

else if finalDict.count > 0 {
        print(section)
        let dictKey =  selectedValueArray[section]
        print(dictKey)
        if self.finalDict.object(forKey: String(describing: dictKey)) != nil {          
           let diaryRowArray = self.finalDict.object(forKey:  String(describing: dictKey)) as! NSArray
           print(diaryRowArray.count)
           print(diaryRowArray)
                if dictKey >= 3 
                   var tempsection = section
                   tempsection = 0
                   let diarydescription = diaryRowArray[tempsection] as! DiaryModel
                   headerLabel.text = diarydescription.diary_category_name
                   print(headerLabel.text!)
                 }
             }
}

【问题讨论】:

  • 你还有问题还是已经解决了?
  • 不,问题解决了。谢谢
  • 好的,如果其中一个为您解决了问题,您应该将答案标记为正确。

标签: ios swift tableview


【解决方案1】:

如果你知道它肯定会是一个 Int(如果它是一个表格行,它应该是一个),你不能强制将它的大小写为一个 Int

let dictKey = selectedValueArray[section] as! Int
if(dictKey >= 3) {
    print("It's equal to or greater than 3")
}

如果它可能是另一种类型(即字符串),您可以将其转换为 Int,然后检查其是否为 nil

let dictKey = selectedValueArray[section] as? Int

if(dictKey != nil) {
    if(dictKey! >= 3) {
        print("It's equal to or greater than 3")
    }
}

或者第三种选择可能是使用 if let 语句

if let dictKey = selectedValueArray[section] as? Int {
    //check if its greater than 3
    if(dictKey >= 3) {
        print("It's equal to or greater than 3")
    }
}

编辑: 如果您的数组是数字字符串数组(即 [“1”、“2”、“3”、“4”]),试试这个

let dictKey = Int(selectedValueArray[section]) 

如果您的数组是任何类型的数组,那么您需要将其转换为字符串数组,然后将值转换为 int。

let stringArray = selectedValueArray as! [String]
let dictKey = Int(stringArray[section])

【讨论】:

  • “不工作”让 dictKey = selectedValueArray[section] as!诠释
  • 那条线的问题是什么?
  • " let dictKey = selectedValueArray[section] as!Int " 当我执行它时显示错误,例如 " 无法将类型 'Swift._NSContiguousString' (0x1130b10e8) 的值转换为 'NSNumber' (0x110322488) ." 我想检查为: if dictkey > 3 { var tempsection = section tempsection = 0 let diarydescription = diaryRowArray[tempsection] as! DiaryModel headerLabel.text = diarydescription.diary_category_name print(headerLabel.text!) }
  • 好的,错误是说它是一个不能转换为 Int 的字符串。那么,我认为 selectedValueArray 是一个具有字符串值的数组吗?
  • 是 selectedValueArray 有字符串值。那么现在我该怎么办?
【解决方案2】:

您无法在dictKey >= 3 之间进行比较,因为 3 是整数,而 dictKey 是 Any。您必须输入 cast dictkey 为:

guard let dictKey = selectedValueArray[section] as? Int else {return} 

那你可以比较一下。

【讨论】:

  • 添加您的建议行后,我如何检查 dictkey 是否大于 3?
  • 演员?为什么不将selectedValueArray 描述为var selectedValueArray: [Int]
  • 现在你可以检查你现在的表现'if dictKey >= 3',因为现在两者都是 Int。
  • selectedValueArray 是我的可变数组,我存储从 tableview 中选择的值。在表头方法中,我使用 dictKey 一一检查 selecteValueArray 的值,例如“let dictKey = selectedValueArray[section]”,现在我想检查 dictkey 是否大于 3,因为我提到了有问题的代码,以便我打印节标题
  • 所以只要你想测试它就应用条件。
猜你喜欢
  • 2015-10-04
  • 1970-01-01
  • 1970-01-01
  • 2017-09-02
  • 2016-09-20
  • 2015-08-12
  • 2015-10-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多