【问题标题】:Binary operator cannot be applied to operands of type int and int? Swift 3二元运算符不能应用于 int 和 int 类型的操作数?斯威夫特 3
【发布时间】:2019-12-06 05:52:59
【问题描述】:

我是 swift 新手,我已经尝试了几个小时这个问题。在我的代码下方:

if filteredCustomReqList != nil { /* [1] error in this line */
    for i in 0..<filteredCustomReqList?.count {
        tempObj = filteredCustomReqList[i] as! [AnyHashable: Any]
        bezeichString = tempObj?["bezeich"] as! String

        specialRequestLabel.text = ("\(filteredString), \(bezeichString!)")
        print (bezeichString!)
    }
}

错误说:

binary operator cannot be applied to operands of type int and int?

在哪里:

var filteredCustomReqList: [Any]? /* = [Any]() */

如果我使用var filteredCustomReqList: [Any] = [Any](),错误就消失了,但我的 if 条件始终为真。如何得到这个修复?我已经阅读了this,但它与我的情况不同(它的intCGFloat)。

任何回答和建议都会对我有所帮助。提前致谢

【问题讨论】:

  • 错误似乎出现在for 行,而不是if 行,对吗?

标签: swift


【解决方案1】:

您可以使用可选绑定if let 来解开filteredCustomReqList 可选变量。

var filteredCustomReqList: [Any]?

if let filteredCustomReqList = filteredCustomReqList {
    for i in 0..<filteredCustomReqList.count {
        tempObj = filteredCustomReqList[i] as! [AnyHashable: Any]
        bezeichString = tempObj?["bezeich"] as! String

        specialRequestLabel.text = ("\(filteredString), \(bezeichString!)")
        print (bezeichString!)
    }
}

【讨论】:

  • 抱歉回复晚了。我一直在尝试所有答案及其对我的工作。但最后,我将这个答案用于我的代码。感谢您的每一个精彩回答
  • 我很高兴听到这个消息! :)
【解决方案2】:

您应该使用可选绑定,这样for 行中就没有可选绑定。

if let list = filteredCustomReqList {
    for i in 0..<list.count {
    }
}

使用更好的for 循环会更好:

if let list = filteredCustomReqList {
    for tempObj in list {
        bezeichString = tempObj["bezeich"] as! String
    }
}

但要做到这一点,请正确声明 filteredCustomReqList

var filteredCustomReqList: [[String: Any]]?

这使它成为一个数组,其中包含具有String 键和Any 值的字典。

【讨论】:

  • 我希望 for 循环在尝试迭代 nil 可选序列时没有任何作用
  • @Alexander 但是 Swift 是专门为避免这种功能而设计的。如果你想要这种行为(“忽略”零点),请使用 Objective-C。
  • 然后允许一些其他语法将条件绑定引入for 循环,而不需要整个 if 语句
  • @Alexander 您可以使用 nil-coalescing 运算符:for i in 0 ..&lt; (myArray?.count ?? 0)
【解决方案3】:

这条线看起来很可疑:

for i in 0..<filteredCustomReqList?.count {

特别是,filteredCustomReqList?.count 的类型为 Int?Int 可选),由于可选链接。也就是说,如果数组 filteredCustomReqList 不是 nil,它会给出其 count 属性的值(即它的元素数)。但是,如果filteredCustomReqListnil,那么就会传播,filteredCustomReqList?.count 也是nil

为了包含这两种可能性,Swift 使用可选类型Int?(它可以表示有效的Int 值和nil)。 它与Int不等价,因此不能用于包含两个Ints 的表达式(如for 循环中的范围) .

您不能将Int? 作为 for 循环范围的上限;这没有意义。您应该在循环之前解开数组:

if let count = filteredCustomReqList?.count {
    // count is of type "Int", not "Int?"
    for i in 0..<count {
        // etc.

【讨论】:

    【解决方案4】:

    首先,与其他答案一样,您应该在使用之前解开可选值。

    请始终牢记,在 swift 中不要仅将 Optional 值视为值本身的一种特殊类型。它们是完全不同的东西。

    一个 Optional 是一个 Optional 的结构,它关联一个 T 的值,如果它存在的话,它看起来是:

    enum Optional< T > {
        case NIL
        case SOME(T)
    }
    

    实际上,在 Swift 中,您可能会更好地执行上述操作:

    // if you don't need return any thing you can just use forEach
    filteredCustomerReqList?.map { obj in 
        tempObj = obj as? [AnyHashable: Any]
        bezeichString = tempObj?["bezeich"] as? String
        specialRequestLabel.text = ("\(filteredString),\(bezeichString)")
        print(bezeichString)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-02
      • 1970-01-01
      • 1970-01-01
      • 2016-09-20
      • 2015-08-12
      • 2015-10-22
      • 2015-10-04
      • 1970-01-01
      相关资源
      最近更新 更多