Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare 2 objects for equality in Objective-C

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

like image 354
user339076 Avatar asked May 12 '10 08:05

user339076


People also ask

How do you compare objects in Objective C?

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.

How do you compare two objects are equal?

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.

Can you use == for objects?

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.

Can we compare two objects in C#?

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.


1 Answers

It's pretty similar!

if ([object1 isEqual:object2])

see the NSObject protocol documentation.

like image 82
deanWombourne Avatar answered Oct 21 '22 18:10

deanWombourne