Waleed Rahman
Waleed Rahman

Reputation: 163

Swift - Operator == Cannot be applied to two Int operands

This is freaking me out and is super-annoying. I've had similar issues before but don't know how I fixed it.

So, I have this condition

for jfa:Dictionary<String, AnyObject> in jfaDict {
    if Int(jfa[JobType.JobNoKey]) == jobNo
    {
        //Some stuff to do here
    }
}

JobType.JobNoKey is a string while jobNo is an Int

And I'm getting the following error:

Binary operator '==' cannot be applied to two Int operands

What does this mean?

Upvotes: 2

Views: 2614

Answers (2)

saurabh
saurabh

Reputation: 6775

Try to typecast as Int first before making the comparison

if let value = jfa[JobType.JobNoKey] as? Int {
  //value has an Int now
  if value == jobNo
  {
    //Some stuff to do here
  }
} else {
//casting as Int failed, AnyObject could not be converted to Int
//Some other stuff to do here
}

Upvotes: 0

ScottyB
ScottyB

Reputation: 2487

You need to unwrap jfa[] first:

if jfa[JobType.JobNoKey] as? Int == jobNo {
    //Some stuff to do here
}

Upvotes: 3

Related Questions