Binary Operator Cannot Be Applied to Operands of Type Int and Int? Swift 3

Binary operator '.. ' cannot be applied to operands of type 'Int' and 'Int?'

Int? is optional, so Xcode is telling you you are trying to use a value that may or may not be there.

You can use the following to avoid a crash if the value is nil:

for i in 0..<(gymdays?.count ?? 0) {

}

Binary operator ' ' cannot be applied to operands of type 'Double?' and 'Int'

The problem here is that percent constant is an optional. Swift 3 has removed comparisons between an optional and a non-optional with the same base type. Conversion between Int literal (0) and Double type is implicit (so this is not a problem here).

You can either unwrap the optional:

if let percent = percent, percent < 0 {
}

or use ?? operator like this:

if ((percent ?? 0) < 0) {
}

You could also use nil coalescing operator in the definition of the percent constant (if that is appropriate for your use case¹):

let percent = Double(data.percent) ?? 0

¹ Note that by doing so you will change the flow of the program when percent is nil (the first branch for percent==0 condition will be executed instead of the last else branch).

Binary operator cannot be applied to operands of type Int and String - Swift 2.3 - Swift 3.2 conversion error

Error itself says it's different types Int and String.

You can need to typecast one or another in same form and them compare.

if (String(error?.code)!) == "-112"){
print("hello")
}

Error: Binary operator ' =' cannot be applied to operands of type 'Int?' and 'Int'

The documentation you need to look into is the one about the "DateComponents" data type, and if you're not familiar with optionals you need to read up on optionals as well.

In a nutshell, the DateComponents data type may not always contain a value for all the components so each member is an optional Int?. This means that they could contain nil.

Because of this you cannot directly compare the date components with a non-optional value. You have to "unwrap" them.

If you are 100% certain that there will always be a .second component, you can force unwrap the value by adding an exclamation point:

if differenceOfDate.second! <= 0

Binary operator '+=' cannot be applied to operands of type.

  1. The reason is that you are trying to use += to two objects of different types,
    one is array of Fruits and another is Optional ([Fruits]? means it
    is optional).

  2. Yes, it is an array.

    2.1 Yes

    2.1.1 You can add another elements by using yourArray.append(object)



Related Topics



Leave a reply



Submit