Comparing Objects in Objective C

Autor: | Última modificación: 2 de noviembre de 2022 | Tiempo de Lectura: 2 minutos
Temas en este post:

Comparing Objects in Objective C: Object Equality in Cocoa & Objective C

Cocoa defines several methods for comparing objects in Cocoa:

  • isEqual:
  •  isEqualToClassName

isEqual: compares the receiver to an object and will return NO if the object doesn’t belong to the same class as the receiver.

isEqualToClassName: compares the iVars of two objects of the same class.

Is nil equal to nil? Sending messages to nil

This is kind of a philosophical issue: if you consider nil to be the equivalent of an unknown value, then 2 unknown values are not necessarily the same. This seems to be the approach of the Cocoa designers.

I consider nil to be the object equivalent of zero, a null value. Therefore, two nil objects should be equal. And this is were you might get into trouble.

Any method sent to nil will get back the type equivalent of 0 (NO, 0, nil). This is a problem when sending isEqual to an object that happens to be nil. In this case you will always get NO, even if both objects are nil, and IMHO should be considered equal.

Overriding isEqual: and creating your own isEqualToClassName:

The recommended way to override, that solves the nil receiver issue is:

  • check for pointer equality (gets rid of the nil receiver problem)
  •  check if parameter is nil or parameter is not of the same class
  • otherwise, delegate to the corresponding isEqualTo

if you know both objects are of the same class and are not nil, ALWAYS use isEqualToClassName:

isEqual: & isEqualToClassName: code snippet

- (BOOL)isEqual:(id)other {
    if (other == self)
        return YES;
    if (!other || ![other isKindOfClass:[self class]])
        return NO;
    return [self isEqualToWidget:other];
}

- (BOOL)isEqualToWidget:(MyWidget *)aWidget {
    if (self == aWidget)
        return YES;
    if (![(id)[self name] isEqual:[aWidget name]])
        return NO;
    if (![[self data] isEqualToData:[aWidget data]])
        return NO; 

    return YES;
}

Fernando Rodríguez

Sígueme en twitter.
Cursos de desarrollo iPhone

[email protected]

¿Trabajo? Aprende a programar y consíguelo.

¡No te pierdas la próxima edición del Aprende a Programar desde Cero Full Stack Jr. Bootcamp!

 

Prepárate en 4 meses, aprende las últimas tecnologías y consigue trabajo desde ya. 

 

Solo en España hay más de 120.400 puestos tech sin cubrir, y con un sueldo 11.000€ por encima de la media nacional. ¡Es tu momento!

 

🗓️ Próxima edición: 13 de febrero

 

Reserva tu plaza descubre las becas disponibles.