Strings in Cocoa: Part I
Subject:   Comparing @"..." type NSStrings
Date:   2001-07-06 15:30:29
From:   halliday
Incidentally, due to the uniqueness properties imposed by NSString upon NSString constants (@"..." type NSStrings), the example code:

NSString *string1 = @"A String";
NSString *string2 = @"A String";

BOOL result = string1 == string2;

evaluates to "yes" rather than "no". The compiler recognizes the second NSString to be the same as one it has already seen and simply reuses it. Since these NSStrings are immutable, there is no danger in doing so.

In fact, there are aspects of Cocoa that strongly rely upon this behavior.

1 to 1 of 1
  1. Michael Beam photo Comparing @"..." type NSStrings
    2001-07-06 17:02:13  Michael Beam | O'Reilly Author [View]

    • Comparing @"..." type NSStrings
      2001-07-07 01:18:41  halliday [View]

    • Comparing @"..." type NSStrings
      2001-07-07 00:47:41  halliday [View]

1 to 1 of 1