John
John

Reputation: 1015

Ambiguous reference to "<" inside conditional | Swift

I would like to create a conditional that presents another view controller based on the results of JSON data:

The JSON structure looks like the following:

struct TheStructure: Codable {
    var NUM1, NUM2: String

    enum CodingKeys: String, CodingKey {
        case NUM1 = "Number1"
        case NUM2 = "Number2"
    }
}

The issue is the API providing the info outputs NUM1 and NUM2 as string for example instead of 1 it does "1". This forces me to decode both values as strings and cause the conditional below to not work. How can this be addressed?

The error I get is:

Ambiguous reference to operator function '<'

The value of portfolio is the following let structureVariable: TheStructure

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        if structureVariable.NUM1 < 1 {

            let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Del1") as! TableViewController1
            let navigationController = UINavigationController(rootViewController: controller)
            self.present(navigationController, animated: true, completion: nil)
            tableView.deselectRow(at: indexPath, animated: false)

        } else {

            let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Del2") as! TableViewController2
            let navigationController = UINavigationController(rootViewController: controller)
            self.present(navigationController, animated: true, completion: nil)
            tableView.deselectRow(at: indexPath, animated: false)

        }
}

Upvotes: 1

Views: 202

Answers (2)

Shehata Gamal
Shehata Gamal

Reputation: 100503

You can't compare a string with int , so replace

if structureVariable.NUM1 < 1 {

with

if let res = Int(structureVariable.NUM1) , res < 1 {

Or this if you sure 100% value will be Int

if Int(structureVariable.NUM1)! < 1 {

A better logic to do

guard let res = Int(structureVariable.NUM1) else { return } 
if res < 1 {
}
else {
}

Upvotes: 1

Marina Aguilar
Marina Aguilar

Reputation: 1189

You can cast the type to Int like this:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

 if let num = Int(structureVariable.NUM1), num < 1 {

       let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Del1") as! TableViewController1
       let navigationController = UINavigationController(rootViewController: controller)
       self.present(navigationController, animated: true, completion: nil)
       tableView.deselectRow(at: indexPath, animated: false)

  } else {

      let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Del2") as! TableViewController2
      let navigationController = UINavigationController(rootViewController: controller)
      self.present(navigationController, animated: true, completion: nil)
      tableView.deselectRow(at: indexPath, animated: false)

      }
}

Upvotes: 0

Related Questions