Re: NSTextField value changes after tab-through
Re: NSTextField value changes after tab-through
- Subject: Re: NSTextField value changes after tab-through
- From: Daniel Staal <email@hidden>
- Date: Mon, 04 Jun 2007 17:58:21 -0400
--As of June 4, 2007 3:43:56 PM -0500, Matthew Stone is alleged to have
said:
I have an app with several NSTextFields (the single-line kind you can
type in) that are initially set to an empty string (@""). Later in my
program, I try to check whether or not the field is empty using the IF
statement:
if( [textField stringValue] == @"" )
// do something
--As for the rest, it is mine.
This one is the correct value (I believe), but you are making a basic
mistake on how Cocoa works: NSString is an object, not a primitive type.
As such, what you are comparing above is _a pointer to an object_ and _a
pointer to another object._
Pointers are only equal if they are the same pointer. Even @"" == @"" is
not guaranteed to work as it would at first glance appear: those could be
different objects that happen to have all the same properties. (Though the
compiler probably has them pointing to the same place. Just don't count on
it.)
What you need is a way to compare the _properties_ of two different objects.
Luckily, Apple has provided what you need: The NSString method
isEqualToString:
So the above code should read:
if ( [[textField stringValue] isEqualToString:@""] )
// Do something.
Daniel T. Staal
---------------------------------------------------------------
This email copyright the author. Unless otherwise noted, you
are expressly allowed to retransmit, quote, or otherwise use
the contents for non-commercial purposes. This copyright will
expire 5 years after the author's death, or in 30 years,
whichever is longer, unless such a period is in excess of
local copyright law.
---------------------------------------------------------------
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden