Re: Status of @"" strings
Re: Status of @"" strings
- Subject: Re: Status of @"" strings
- From: Chris Kane <email@hidden>
- Date: Thu, 12 Jul 2001 13:42:25 -0700
On Thursday, July 12, 2001, at 12:12 PM, Fritz Anderson wrote:
If one is looking for a framework-defined symbol (e.g.,
NSFontAttributeName), is it safe to rely solely on the equality of the
object addresses? From what you say, I am guessing no, because (a)
even if the two symbol references originated in the same application,
it's only a "happy accident" that the two references should be to the
same object; (b) symbols loaded at runtime, such as from decoded
NSDictionaries, don't get interned; and (c) there's always some
smartaleck who writes @"NSFontAttributeName" instead of
NSFontAttributeName.
Well, a constant global variable has only a single value regardless of
where it is referenced, so uniquing doesn't really come into that; a
reference to NSFontAttributeName will be pointer equal to a reference to
NSFontAttributeName somewhere else. AND, you don't really know that any
particular constant global string variable is initialized with @"".
However, the answer to the question you're trying to get at is indeed,
no. You should always use -isEqual: or -isEqualToString: or whatever
variation on that the class might have. (c) is certainly true, but
tricky since the value of a constant string global is not always the
name of the symbol.
You forgot one: (d) strings can be created with methods, like
+stringWithCharacters:. Such strings should be treated equal to strings
created with @"", if they have equal contents. The technique that was
used to make them should be immaterial, so -isEqual* is necessary, even
if @"" strings were uniqued.
Chris Kane
Cocoa Frameworks, Apple