-2

I have the following class:

import UIKit


class SomeClass: Equatable {
    var Obj: Any
    
    init(Obj:Any) {
        self.Obj = Obj
    }
    
    static func == (lhs: SomeClass, rhs: SomeClass) -> Bool {
        return lhs.Obj == rhs.Obj
    }
}

But I'm getting the following error:

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

On this line:

return lhs.Obj == rhs.Obj

Any of you knows why I'm getting this error? or if there is a way around this?

I'll really appreciate your help.

user2924482
  • 8,380
  • 23
  • 89
  • 173
  • 2
    The error tells you exactly the problem. `==` cannot be applied to `Any`. You almost certainly don't want Any here, anyway (Any is a very rare type in good Swift, and always a bothersome type). You should likely redesign SomeClass to remove the Any. – Rob Napier Sep 21 '20 at 18:12
  • There is another generic way or type I can use beside `Any`? – user2924482 Sep 21 '20 at 18:13
  • 2
    Completely depends on what problem you're solving. What is Obj and SomeClass for? The broad answer is to make SomeClass generic (`class SomeClass: Equatable`), but that introduces various other restrictions, so it depends on the exact situation. – Rob Napier Sep 21 '20 at 18:13
  • 5
    `Any` is just about always a bad smell. In any case this appears to be an x–y question. The real question should be about whatever problem this class was intended to tackle. – matt Sep 21 '20 at 18:15
  • With `class SomeClass: Equatable {` still get the same error. I just want to be able to assign any kind object. I was trying to implement this as generic as posible – user2924482 Sep 21 '20 at 18:18
  • 1
    What if the things are not equatable? For example, two infinite sequences? What about two functions? Are they equal if they return the same values? If they have the same op codes? If they're created from the same source code. Any is a very broad type including all kinds of things that you're probably not thinking of as "objects." "As generic as possible" doesn't mean anything here. You need to work from your actual use cases. – Rob Napier Sep 21 '20 at 18:19
  • I was not suggesting you just replace the first line of code. I meant redesigning the type to use generics rather than Any. If you want a longer discussion of how to deal with Equatable in a protocol-driven way, see https://www.youtube.com/watch?v=DXwJg0QTlZE (start around 37:30). But Any is almost always the wrong tool. Start with your actual use case; extract generic code from where it duplicates. – Rob Napier Sep 21 '20 at 18:21
  • That said, if you mean that Obj must be a *class* (rather than any type), then you can use `AnyObject` and `===` to test for "the same memory location." This is almost always bad Swift, but it is legal. – Rob Napier Sep 21 '20 at 18:25
  • @RobNapier I think they should allow `extension Any:Equatable { /* ... */ }` in the next swift version :-) – Andreas Oetjen Sep 21 '20 at 19:18

1 Answers1

-2

check the equality of each property of your custom class in the Equatable protocol function as below

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
            let objA = someClass(name: "Name1", age: 25)
            let objB = someClass(name: "Name2", age: 25)
            print(objA == objB)
    }
}

class   someClass: Equatable {
    static func == (lhs: someClass, rhs: someClass) -> Bool {
        return lhs.name == rhs.name && lhs.age == rhs.age
    }
    
    let name: String
    let age: Int
    
    init(name: String, age: Int){
        self.name = name
        self.age = age
    }
}

Result