0

I'm trying to verify if an object that comes from an API already exists in my array of objects, but I'm not being able to do that verification, Xcode keeps telling me that

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

and my code is:

var cidades = [Cidades]()

    func findAllCidades(completion: @escaping CompletionHandler) {
        Alamofire.request(URL_CIDADESATIVAS, method: .get, parameters: nil, encoding: JSONEncoding.default).responseJSON { (response) in

            if response.result.error == nil {
                guard let data = response.data else {return}


                if let json = try? JSON(data: data) {
                    let results = json["results"].array

                    for item in results! {
                        let name = item["nome"].stringValue
                        let estado = item["estado"].stringValue
                        let id = item["id"].intValue

                        let cidade = Cidades(id: id, nome: name, estado: estado)
                        if(!self.cidades.index(where: { $0 == cidade})){
                            self.cidades.append(cidade)
                        }


                    }
                    completion(true)

                }

            } else {
                completion(false)
                debugPrint(response.result.error as Any)
            }
        }
    }

3 Answers 3

1

cidade would not be indexed in self.cidades because of cidade in a newly created object. If you try to check then you need to compare a specific value.

let cidade = Cidades(id: id, nome: name, estado: estado)
if self.cidades.contains(where: { $0.id == cidade.id}) {
      self.cidades.append(cidade)
}

Hope this will help.

Sign up to request clarification or add additional context in comments.

Comments

1

Equatable protocol can be compared for equality using the equal-to operator (==)

Inside your model class past below code for Equatable

extension Cidades : Equatable {
    static func ==(lhs: Cidades, rhs: Cidades) -> Bool {
        return (lhs.nome == rhs.nome && lhs.estado == rhs.estado && lhs.id == rhs.id)
    }

And you can check object exists or not using your custom Equatable(==) function

let cidade = Cidades(id: id, nome: name, estado: estado)
        if self.cidades.filter({ c1 in c1 == cidade}).count > 0 {
            // Found
        }else{
            // Not found
        }
}

Here is good resource by Apple

Comments

1

You can check with the specific value the array contains with this, like if array contains cidade object with the same id then if condition will be true.

if self.cidades.contains(where: { $0.id == cidade.id}) {
     // found
} else {
     // not
}

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.