NSString: isEqual vs. isEqualToString

42,773

Solution 1

isEqual: compares a string to an object, and will return NO if the object is not a string. isEqualToString: is faster if you know both objects are strings, as the documentation states:

Special Considerations

When you know both objects are strings, this method is a faster way to check equality than isEqual:.

isEqualTo<Class> is used to provide specific checks for equality. For instance; isEqualToArray: checks that the arrays contain an equal number of objects, and that the objects at a given index return YES for the isEqual: test.

Solution 2

Also, for writing your own -isEqual: and -isEqualTo<Class>: methods, the convention is to allow nil arguments for -isEqual: and raise an exception for nil arguments to -isEqualTo<Class>:

Solution 3

Expanding on @Abizern and @Jonathan Dann answers, both isEqual and isEqualToString work with nil values.

- (void)testStringEqual {
    NSString *string = nil;

    STAssertFalse([string isEqual:@"test"], @"NSString isEqual");
    STAssertFalse([string isEqualToString:@"test"], @"NSString isEqualToString");

    // Note that these both return NO
    STAssertFalse([string isEqual:nil], @"NSString isEqual");
    STAssertFalse([string isEqualToString:nil], @"NSString isEqualToString");

    string = @"test";

    STAssertTrue([string isEqual:@"test"], @"NSString isEqual");
    STAssertTrue([string isEqualToString:@"test"], @"NSString isEqualToString");

    STAssertFalse([string isEqual:nil], @"NSString isEqual");
    STAssertFalse([string isEqualToString:nil], @"NSString isEqualToString");
}

Solution 4

My guess is that it provides a slight performance enhancement, as isEqualToString: won't have to type-check what's passed in.

Solution 5

I highly recommend this. The performance benefits of isEqualToString are basically negligible for most applications. But there are two other distinctions the author mentions:

  • Type safety
  • The way nil is handled
Share:
42,773
Jaka Jančar
Author by

Jaka Jančar

Updated on July 08, 2022

Comments

  • Jaka Jančar
    Jaka Jančar almost 2 years

    What is the difference between isEqual: and isEqualToString:?

    Why are classes adding isEqualTo* methods (isEqualToArray for NSArray, isEqualToData for NSData, ...) instead of just overriding isEqual: ?

  • Mike Abdullah
    Mike Abdullah over 14 years
    I hadn't come across this before, any documentation that you know of?
  • Jaka Jančar
    Jaka Jančar over 14 years
    This doesn't seem to be true for isEqualToString, which just returns NO if you pass in nil.
  • Jonathan Dann
    Jonathan Dann over 14 years
    Interesting, it's documented in the Object Comparison section of the <a href="developer.apple.com/documentation/Cocoa/Conceptual/… Fundamentals Guide</a>
  • Philip007
    Philip007 over 11 years
    Your guess is probably true:)
  • respectTheCode
    respectTheCode almost 11 years
    This is not true. isEqualToString does not raise an exception.
  • cbh2000
    cbh2000 over 10 years
    The Cocoa Fundamentals Guide web page says, "This document may not represent best practices for current development." It's old, apparently.
  • Caro
    Caro over 10 years
    If you believe Aaron Hillegass then there is no performance difference, only a bit of type safty: blog.bignerdranch.com/334-isequal-vs-isequaltostring
  • Abizern
    Abizern over 10 years
    Thanks for the link - useful. Although you're asking us to believe Mark Dalrymple - who I do :)
  • SayeedHussain
    SayeedHussain over 9 years
    I don't see any difference in the way nil is handled by the two. Be nil be the receiver or argument or both.
  • Jared Grubb
    Jared Grubb almost 6 years
    Whatever "this" is no longer exists :/
  • Ben Packard
    Ben Packard almost 6 years
    Thanks @JaredGrubb, I found the new URL.
  • Max
    Max about 5 years