In Java you can write an if statement like this:
if(object1.equals(object2)){ // Do something.... }
How can I code the same logic in Objective-C? I basically want to compare 2 of any one type of objects, such as 'Text Fields', 'Text Views', etc.
Thank you.
Shakeel
In Objective-C, an object's identity is tied to its memory address. When you use the == operator to compare two objects in Objective-C, you're checking to see if they point to the same location in memory.
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. For example, the expression obj1==obj2 tests the identity, not equality.
The equality operator or "==" compares two objects based on memory reference. so "==" operator will return true only if two object reference it is comparing represent exactly same object otherwise "==" will return false.
The most common way to compare objects in C# is to use the == operator. For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise. For reference types other than string, == returns true if its two operands refer to the same object.
It's pretty similar!
if ([object1 isEqual:object2])
see the NSObject
protocol documentation.
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