I have several "Any" value types that I want to compare.
var any1: Any = 1 var any2: Any = 1 var any3: Any = "test" var any4: Any = "test" print(any1 == any2) print(any2 == any3) print(any3 == any4)
Using the == operator shows an error:
"Binary operator '==' cannot be applied to two 'Any' (aka 'protocol<>') operands"
What would be the way to do this ?
As a general rule, C will not compare two values if they are not the same type and will never implicitly convert a variable to a type with less precision.
Strict equality compares two values for equality. Neither value is implicitly converted to some other value before being compared. If the values have different types, the values are considered unequal. If the values have the same type, are not numbers, and have the same value, they're considered equal.
In Java, the == operator compares that two references are identical or not. Whereas the equals() method compares two objects. Objects are equal when they have the same state (usually comparing variables). Objects are identical when they share the class identity.
Equality and Inequality Operators. The == and === operators check whether two values are the same, using two different definitions of sameness. Both operators accept operands of any type, and both return true if their operands are the same and false if they are different.
We categorized the items into separate types of value and ended up with five that seem to describe most cases quite well. We’ll discuss these in no particular order below. Note: because our personal experience lies mostly with commercial organizations, most of our examples are from there as well.
What are the Main Valuation Methods? When valuing a company as a going concern, there are three main valuation methods used by industry practitioners: (1) DCF analysis, (2) comparable company analysis, and (3) precedent transactions. These are the most common methods of valuation used in investment banking.
A variable on the other hand is an abstraction of a data storage element and can hold values. A flip-flop is a good example of a storage element. Verilog data-type reg can be used to model hardware registers since it can hold values between assignments.
Comparable company analysis (also called “trading multiples” or “peer group analysis” or “equity comps” or “public market multiples”) is a relative valuation method in which you compare the current value of a business to other similar businesses by looking at trading multiples like P/E, EV/EBITDA, or other ratios.
The only way to do this is with a function other than ==
that takes a type parameter, and then compares the values if they are both of that type:
func isEqual<T: Equatable>(type: T.Type, a: Any, b: Any) -> Bool { guard let a = a as? T, let b = b as? T else { return false } return a == b }
Now, using your variables above, you can compare them like this:
var any1: Any = 1 var any2: Any = 1 var any3: Any = "test" var any4: Any = "test" isEqual(type: Int.self, a: any1, b: any2) // true isEqual(type: Int.self, a: any2, b: any3) // false isEqual(type: String.self, a: any3, b: any4) // true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With