Binary operator cannot be applied to operands of type int and int? Swift 3

You should use optional binding so you don't have an optional in the for line.

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

Even better would be to use a better for loop:

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

But to do this, declare filteredCustomReqList properly:

var filteredCustomReqList: [[String: Any]]?

This makes it an array that contains dictionaries that have String keys and Any values.


You can use Optional Binding if let to unwrap filteredCustomReqList Optional variable.

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!)
    }
}

This line looks suspicious:

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

In particular, filteredCustomReqList?.count is of type Int? (Int optional), due to optional chaining. That is, if the array filteredCustomReqList is non-nil it gives the value of its count property (i.e., its number of elements). But if filteredCustomReqList is nil, that is propagated and filteredCustomReqList?.count is nil too.

In order to encompass both possibilities, Swift uses the optional type Int? (which can represent both valid Int values and nil). It is not equivalent to Int, and thus can not be used in an expression that expexts two Ints (like the range in your for loop).

You can't have Int? as the upper bound your for loop range; it doesn't make sense. You should unwrap the array before looping:

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

Tags:

Swift